enrm
enrm

Reputation: 705

Shared jar and noclassdeffound java

Im running a debian java server that needs to send and receive objects of type EventObject and PostObject (e.g serializable). These have been placed in a .jar file SharedModels.jar and are used both in client and server. On the windows installation (Eclipse), using

import Models.EventObject; import Models.PostObject; works fine (including external Jar through Eclipse).

It compiles fine, but when the method is called on the server , the server doesn't recognize the class anymore. This is the output:

Exception in thread "Thread-0"     java.lang.NoClassDefFoundError: Models/PostObject
at server.Database.getPosts(Database.java:101)
at server.ServerThread.run(ServerThread.java:47)
Caused by: java.lang.ClassNotFoundException:     Models.PostObject
    at     java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at  java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more

I've understood this is because JVM does recognize the class at compiletime but not during runtime.. OR something is wrong with classpath. Does the name of the actual .jar need to have any naming to fid the package contained within? What do I need to do to fix this?

Upvotes: 0

Views: 69

Answers (2)

Mohan Raj
Mohan Raj

Reputation: 1132

This is thrown when at compile time the required classes are present , but at run time the classes are changed or removed or class's static initializes threw exceptions. It means the class which is getting loaded is present in classpath , but one of the classes which are required by this class , are either removed or failed to load by compiler .So you should see the classes which are dependent on this class

Upvotes: 0

Praeterii
Praeterii

Reputation: 340

If your Models/PostObject is a class from some jar, then make sure that the class is included in exported jar/war (simply open it with tool like 7zip and manually confirm that the needed class is there).

How to create a jar with external libraries included in Eclipse?

Upvotes: 1

Related Questions