抛出java.lang.ClassNotFoundException运行时,Java的罐子(尚未解决) [英] java.lang.ClassNotFoundException when running java -jar (still unsolved)
问题描述
我使用Ant来构建我的的build.xml
文件,它编译好的,但后来得到一个运行时 java.lang.NoClassDefFoundError的
通过运行所产生的罐子时, Java的罐子my_jar.jar
。看起来这来了很多,但没有一个相关问题的解决方案的工作对我来说。
我对的javac
仅包含 /usr/local/lib/libthrift.jar
和主类路径的.java
文件导入了一堆旧货包,如 org.apache.thrift.transport.TTransportException
。
当我尝试通过运行程序:
Java的罐子MyClass.jar
,我得到的错误:
异常线程main** ** java.lang.NoClassDefFoundError的:组织/阿帕奇/节俭/运输/ TTransportException
抛出java.lang.ClassNotFoundException:引起org.apache.thrift.transport.TTransportException
在java.net.URLClassLoader的$ 1.run(URLClassLoader.java:200)
在java.security.AccessController.doPrivileged(本机方法)
在java.net.URLClassLoader.findClass(URLClassLoader.java:188)
在java.lang.ClassLoader.loadClass(ClassLoader.java:307)
在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301)
在java.lang.ClassLoader.loadClass(ClassLoader.java:252)
在java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
找不到主类:** ** MyClass的。计划将退出。
下面是到目前为止,不工作我已经试过的东西:
-
喜欢的java命令行上添加一个标志
-cp /usr/local/lib/libthrift.jar
-jar my_jar.jar
,结果是相同的错误如上 -
添加
&放大器; LT;属性名=类路径VALUE =./:/ USR / local / lib目录/ libthrift.jar/>
里面我罐子的清单>
标签,结果是相同的错误如上 -
添加
-Xbootclasspath / A:/usr/local/lib/libthrift.jar:./
以java命令行。它解决了第一个错误,但不同的错误出现:异常线程mainjava.lang.NoClassDefFoundError的:组织/阿帕奇/ log4j的/记录器
在org.apache.thrift.transport.TServerSocket< clinit>(TServerSocket.java:36)
在MyClass.start(来源不明)
在MyClass.main(来源不明)
您的帮助是AP preciated!谢谢你。
编辑:
如果我注释掉code实例化缺失的类,但离开了进口,code执行罚款。
编辑:
我把我的java类到服务器,并在清单属性与服务器所引用的MainClass,但没有任何修复。
找不到主类:MyClass的
该错误似乎实际上与你的清单
其中:
- 可能没有一个完整的类路径
类路径
:看的这个方法文档
最好的解决办法,当你有一个罐子是尝试包括所需的jar到舱单申报。
块引用>清单-版本:1.0
类路径:
customer_client.jar
mailer_client.jar
signon_client.jar
- 或可能不会充分地定义你的'my_jar.jar内MainClass。
请参阅此方法文档:
<目标名称=罐子依赖=编译>
<删除文件=hello.jar/>
<删除文件=MANIFEST.MF/>
<清单文件=MANIFEST.MF>
<属性名=内置到VALUE =$ {user.name}/>
<属性名=主类VALUE =howto.Hello/>
< /清单> <罐子destfile =hello.jar
BASEDIR =。
包括=** / *。班
清单=MANIFEST.MF
/>
< /目标与GT;在
<属性名=主类VALUE =howto.Hello/>
需要指定的完整路径(包)MainClass
,而不仅仅是MainClass
。如果你的主类是在默认包(无名包),我不知道它可以通过加载器(请参阅本<一个被引用href=\"http://stackoverflow.com/questions/859603/java-is-there-a-tool-to-make-$c$c-in-a-3rd-party-jar-forward-compatible-1-4\">SO问题)结果
因此,移动你的JarRunner
成一个包,并在&LT适当其声明;属性名=主类VALUE =myPackage.JarRunner /方式&gt;
元素I'm using ant to build my
build.xml
file, it compiles ok, but then getting a runtimejava.lang.NoClassDefFoundError
when running the resulting jar via "java -jar my_jar.jar
". It seems like this comes up a lot but none of the related questions' solutions worked for me.My classpath for
javac
contains only "/usr/local/lib/libthrift.jar
" and the main.java
file imports a bunch of thrift packages such asorg.apache.thrift.transport.TTransportException
.When I try running the program via:
java -jar MyClass.jar
, I get the error:
Exception in thread "main" **java.lang.NoClassDefFoundError**: org/apache/thrift/transport/TTransportException Caused by: java.lang.ClassNotFoundException: org.apache.thrift.transport.TTransportException at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) Could not find the main class: **MyClass**. Program will exit.
Here are the things I've tried so far that don't work:
adding a flag on the command line like "java
-cp /usr/local/lib/libthrift.jar
-jar my_jar.jar
", the result is the same error as aboveadding
<attribute name="Class-Path" value="./:/usr/local/lib/libthrift.jar"/>
inside my jar'smanifest>
tag, the result is the same error as aboveadding
-Xbootclasspath/a:/usr/local/lib/libthrift.jar:./
to the java command line. it solves the first error but a different error comes up:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger at org.apache.thrift.transport.TServerSocket.<clinit>(TServerSocket.java:36) at MyClass.start(Unknown Source) at MyClass.main(Unknown Source)
Your help is appreciated! Thanks.
EDIT:
If I comment out the code that instantiates the missing classes but leave the imports, the code executes fine.
EDIT:
I moved my java classes to a server and referenced the MainClass with the server in the manifest attribute, but that didn't fix anything.
解决方案Could not find the main class: MyClass
The error seems actually related to your
MANIFEST
which:
- may not have a complete classpath
Class-Path
: see this HowToThe best solution when you have a jar is to try to include the required jars into the manifest declaration.
Manifest-Version: 1.0 Class-Path: customer_client.jar mailer_client.jar signon_client.jar
- or may not define adequately the MainClass within your 'my_jar.jar'.
See this HowTo:
<target name="jar" depends="compile"> <delete file="hello.jar"/> <delete file="MANIFEST.MF"/> <manifest file="MANIFEST.MF"> <attribute name="Built-By" value="${user.name}"/> <attribute name="Main-Class" value="howto.Hello"/> </manifest> <jar destfile="hello.jar" basedir="." includes="**/*.class" manifest="MANIFEST.MF" /> </target>
the
<attribute name="Main-Class" value="howto.Hello"/>
needs to specify the full path (packages) of theMainClass
, not justMainClass
.If your main class is in the default package (the unnamed package), I am not sure it can be referenced by the loader (see this SO question)
So move yourJarRunner
into a package, and declare it appropriately in the<attribute name="Main-Class" value="myPackage.JarRunner"/>
element.这篇关于抛出java.lang.ClassNotFoundException运行时,Java的罐子(尚未解决)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!