My module uses some class from NetBeans' APIs. It compiles, but I get a NoClassDefFoundError at runtime. Why?
Normally this should not happen because the module build harness tries to protect you from such cases. Still, if it does happen, it could mean
-
your module is trying to use a class, but your module does not declare a dependency on the module that provides that class … or
-
you are declaring a dependency on the right module, but you are accessing a class that is not in one of the packages that module says are public (for use by other modules) … or
-
your module is not a "friend" of the module that provides the class.
If the problem is #1, you need to declare a dependency on the module where the class is (remember that all of NetBeans APIs are modules, and in separate jars - so if it’s the IO API, that’s a module org.openide.io
, if it’s the Window System, that’s a module org.openide.windows
… and so forth).
Setting dependencies is easy - open the Properties for your project, and choose the Libraries page. (Or just get the context menu for the Libraries node under the project in the Projects window.) Click Add and a small dialog opens - just type the name of a class you need to use, and it will filter the list to find the module that provides that class - so you don’t have to memorize a huge list of mappings from classes to modules.
If it’s problem #2, then you are already declaring a dependency, but to get full access to all classes in a module, you need to declare an implementation dependency (DevFaqImplementationDependency). Be sure you really need to use the class you’re trying to use, in this case - it will make your module hard to upgrade because generally it will need to be paired with the exact version of the other module’s JAR that it was built with - if that module is upgraded, your module may end up being disabled.
Also check if class you are trying to use from other module is mark as public to outside modules (right-click on module and select Properties, then API Versioning and in Public Packages check whether package of your class is ticked).
Problem #3 may happen if you change your modules name. If some module declared yours as a friend it will no longer recognize it.
Checking for errors eagerly
For a nice way to resolve all module dependencies at once, to force all of the errors to be exposed simultaneously, just add the following to the command line when starting NetBeans:
-J-Dnetbeans.preresolve.classes=true
The message displayed states that when using this flag, you should not use the -J-Xverify:none
flag (often specified in the IDE configuration file), so you may need to edit the .conf
file to remove the -Xverify option before using the pre-resolve option.