简体   繁体   English

无法从我的jar中加载jar:NoClassDefFoundError

[英]Unable load jar from my jar: NoClassDefFoundError

I have this jar: 我有这个罐子:

/weekly/
  /database.class
  /report.class
/META-INF
  /MANIFEST.MF

The MANIFEST.MF 清单文件

Manifest-Version: 1.0
Main-Class: weekly.report
Class-Path: /root/java

In this jar i used two external jar files, postgresql-9.1-901.jdbc4.jar, javax.mail.jar. 在这个jar中,我使用了两个外部jar文件,分别是postgresql-9.1-901.jdbc4.jar,javax.mail.jar。 I also put those into /root/java . 我也将它们放到/root/java

but when i run it use 但是当我运行它时

java -jar weekly.jar

It shows those two jars class can't find 它表明这两个罐子类找不到

Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/internet/AddressException
Caused by: java.lang.ClassNotFoundException: javax.mail.internet.AddressException
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

even i try to put those two jars to my $CLASSPATH 甚至我尝试将这两个罐子放到我的$ CLASSPATH中

       /usr/java/jdk1.6.0_21/lib:/usr/java/jdk1.6.0_21/jre/lib

It also shows same error result. 它还显示相同的错误结果。 I can successfully run it in my Mac, but when i try to remove it to my server CentOS system ,it shows those NoclassDefFounderror. 我可以在Mac上成功运行它,但是当我尝试将其删除到服务器CentOS系统上时,它会显示那些NoclassDefFounderror。

Finding a JAR file using a classpath is similar to finding a package file in a classpath. 使用类路径查找JAR文件类似于在类路径中查找程序包文件。 The difference is that when you specify a path for a JAR file, you must include the name of the JAR file at the end of the path . 区别在于,当您为JAR文件指定路径时, 必须在该路径的末尾包含JAR文件的名称

So you can try something like this: 因此,您可以尝试执行以下操作:

java -classpath /usr/java/jdk1.6.0_21/lib/javax.mail.jar:/usr/java/jdk1.6.0_21/jre/lib/postgresql-9.1-901.jdbc4.jar -jar weekly.jar

Another option is to put your jars into jre/lib/ext (but I don't think it's a good idea) 另一个选择是将jars放入jre / lib / ext(但我认为这不是一个好主意)

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

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