本文介绍了运行java -jar时的java.lang.ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ant构建我的 build.xml 文件,它编译好了,但后来获得运行时 java.lang.NoClassDefFoundError 通过 java -jar my_jar.jar 运行生成的jar时。看起来好像很多,但没有一个相关问题的解决方案适合我。

I'm using ant to build my build.xml file, it compiles ok, but then getting a runtime java.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.

我的 javac 仅包含 /usr/local/lib/libthrift.jar ,主 .java 文件导入a一堆thrift包,例如 org.apache.thrift.transport.TTransportException

My classpath for javac contains only "/usr/local/lib/libthrift.jar" and the main .java file imports a bunch of thrift packages such as org.apache.thrift.transport.TTransportException.

当我尝试运行程序时通过:

When I try running the program via:

java -jar MyClass.jar

,我收到错误:

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:


  • 在命令行中添加一个标志,如java -cp / usr / local / lib / libthrift。 jar -jar my_jar.jar ,结果与上面的错误相同

  • 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 above

添加 & lt; attribute name =Class-Pathvalue =./:/ usr / local / lib / libthrift.jar/> 在我的jar的清单> 标记内,结果与上面的错误相同

adding <attribute name="Class-Path" value="./:/usr/local/lib/libthrift.jar"/> inside my jar's manifest> tag, the result is the same error as above

-Xbootclasspath / a:/usr/local/lib/libthrift.jar:./ 添加到java命令线。它解决了第一个错误,但出现了另一个错误:

adding -Xbootclasspath/a:/usr/local/lib/libthrift.jar:./ to the java command line. it solves the first error but a different error comes up:

线程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)

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)

编辑:

如果我发表评论在实例化缺失类但保留导入的代码中,代码执行正常。

If I comment out the code that instantiates the missing classes but leave the imports, the code executes fine.

编辑:

我将我的java类移动到服务器并使用manifest属性中的服务器引用MainClass,但是没有修复任何内容。

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

该错误实际上与您的 MANIFEST 有关:

The error seems actually related to your MANIFEST which:


  • 可能没有完整的类路径 Class-Path :请参阅

  • may not have a complete classpath Class-Path: see this HowTo



Manifest-Version: 1.0
Class-Path:
 customer_client.jar
 mailer_client.jar
 signon_client.jar




  • 或者可能没有充分定义'my_jar.jar'中的MainClass。

  • 参见:

    <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>
    

    < attribute name =Main-Classvalue =howto .Hello/> 需要指定 MainClass 的完整路径(包),而不仅仅是 MainClass

    the <attribute name="Main-Class" value="howto.Hello"/> needs to specify the full path (packages) of the MainClass, not just MainClass.

    如果你的主类在默认包中(),我不确定加载程序是否可以引用它(请参阅)

    所以将 JarRunner 移动到一个包中,并在< attribute name =Main中正确声明它-Classvalue =myPackage.JarRunner/> 元素。

    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 your JarRunner into a package, and declare it appropriately in the <attribute name="Main-Class" value="myPackage.JarRunner"/> element.

    这篇关于运行java -jar时的java.lang.ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:28