Issue
- When starting up Liferay, the following errors are observed:
java.lang.NoClassDefFoundErrororjava.lang.ClassNotFoundException.
Environment
- Liferay DXP with deployed custom modules including optional dependencies.
Resolution
- Review the deployed
MANIFEST.MFinside the module or thebnd.bndin the source code and check if the dependency to the package that contains the class is defined asoptional. - Analyze the convenience of removing the
optionalmodifier.
Optional dependencies should be avoided as much as possible. They bypass OSGi validations that ensure a module can be safely started without having classes missing during runtime.
Additional Information
- The simplest module designs rely on normal dependencies. If an optional dependency seems desirable, it's often the case that your module is trying to provide distinct types of functionality. In such a situation, it's best to split your module into multiple modules that each provide smaller, more focused functionality.
- If you install the module when the optional dependency is missing from Liferay DXP's OSGi runtime, your controller component catches a
ClassNotFoundExceptionand logs a warning or info message (or takes whatever other action you implement to handle this case). If you install the optional dependency, refreshing your module triggers the OSGi bundle lifecycle events that trigger your controller'sactivatemethod and your check for the optional dependency. Since your dependency exists, your client component uses it.