简体   繁体   English

共享的Jar和NoClassDefFound Java

[英]Shared jar and noclassdeffound java

Im running a debian java server that needs to send and receive objects of type EventObject and PostObject (eg serializable). 我正在运行一个Debian Java服务器,该服务器需要发送和接收EventObject和PostObject类型的对象(例如,可序列化的)。 These have been placed in a .jar file SharedModels.jar and are used both in client and server. 这些已放置在.jar文件SharedModels.jar中,并且已在客户端和服务器中使用。 On the windows installation (Eclipse), using 在Windows安装(Eclipse)上,使用

import Models.EventObject; 导入Models.EventObject; import Models.PostObject; 导入Models.PostObject; works fine (including external Jar through Eclipse). 工作正常(包括通过Eclipse的外部Jar)。

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. 我了解这是因为JVM确实在编译时识别了该类,但在运行时却没有。.或者类路径有问题。 Does the name of the actual .jar need to have any naming to fid the package contained within? 实际.jar的名称是否需要命名才能找到其中包含的软件包? What do I need to do to fix this? 我需要做什么来解决这个问题?

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). 如果您的Models / PostObject是某个jar中的类,请确保该类包含在导出的jar / war中(只需使用7zip之类的工具将其打开,并手动确认所需的类在其中)。

How to create a jar with external libraries included in Eclipse? 如何使用Eclipse中包含的外部库创建jar?

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 这意味着要加载的类存在于类路径中,但是该类所需的一个类被编译器删除或加载失败,因此您应该看到依赖于该类的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM