0

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?

enrm
  • 645
  • 1
  • 8
  • 22

2 Answers2

1

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?

Community
  • 1
  • 1
Praeterii
  • 340
  • 6
  • 16
  • This should be a non-issue, since I made the jar myself. I'm pretty sure it's there. nonetheless. Will check. The jar is structured so that the package is called Models and the objects are Models/PostObject and Models/EventObject. Is this an issue? – enrm Apr 28 '15 at 09:10
  • Who made the jar has nothing to do with whether it is exported or no. Make sure you are exporting it as "Runnable jar" if you are using eclipse. – Praeterii Apr 28 '15 at 09:18
  • Don't I need a main class for runnable jars to work? What would that main do? I need a different main class on server and client obviously.. – enrm Apr 28 '15 at 14:57
  • How do you run it on server? Is this some sort of webapp? If so then you might either create war instead of jar or add dummy main function. Have you confirmed if classes are included? – Praeterii Apr 28 '15 at 22:03
  • Classes are included afaik, since I compiled it on Windows machine using Eclipse and just sent them over to my other distro. Wouldn't a dummy main cause problems when running the real main ? I'll check out wars.. – enrm May 08 '15 at 12:03
0

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

Mohan Raj
  • 1,104
  • 9
  • 17