Search code examples
javaeclipse-pluginclassloaderclasscastexception

ClassNotFoundException even though the class is included in a jar file included in the buildpath


I am editing an eclipse plugin code. Just a starter so haven't got much idea about it. Just trying to make it run atm.

The program when run throws ClassNotFoundException. I have included the puakma.jar file to the buildpath of the project and also confirmed that the class exists in the jar file inside SOAP package. What could be the possible problem?

The stacktrace is as follows:

java.lang.NoClassDefFoundError: puakma/SOAP/SOAPFaultException
    at puakma.coreide.ServerManager.createServerConnection(ServerManager.java:120)
    at puakma.vortex.dialogs.server.AppSelectionDialog.listApplications(AppSelectionDialog.java:284)
    at puakma.vortex.dialogs.server.AppSelectionDialog$6.run(AppSelectionDialog.java:234)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
Caused by: java.lang.ClassNotFoundException: puakma.SOAP.SOAPFaultException
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 4 more

Solution

  • You need to add correspoinding jar to a Classpath as below for Eclipse plugins:

    1. Open you plugin.xml
    2. Goto Runtime tab
    3. Under classpath section click on "Add" and select the jar file which you have placed in folder under your project (say under lib).
    4. Now clean and build the project

    When you do that it will add corresponding entry to MANIFEST.MF as below:

    Bundle-ClassPath: lib/foobar.jar,
    .
    

    The way you add jars to classpath in eclipse plugins is different from ordinary java projects, since it's based on OSGi.

    You can learn more about classpath for eclipse plugins from this link. Hope this helps.