运行 java -jar 时出现 java.lang.ClassNotFoundException [英] java.lang.ClassNotFoundException when running java -jar

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

问题描述

我正在使用 ant 来构建我的 build.xml 文件,它编译正常,但是在通过运行生成的 jar 时获得运行时 java.lang.NoClassDefFoundErrorjava -jar my_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 文件导入了一个一堆 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

添加<attribute name="Class-Path" value="./:/usr/local/lib/libthrift.jar"/> 在我的 jar 的 manifest> 标签中,结果是和上面一样的错误

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在 org.apache.thrift.transport.TServerSocket.<clinit>(TServerSocket.java:36)在 MyClass.start(来源不明)在 MyClass.main(来源不明)

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:

当您有 jar 时,最好的解决方案是尝试将所需的 jar 包含到清单声明中.

The 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 

  • 或者可能没有在您的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-Class" value="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.

    如果您的主类在默认包中(未命名包),我不确定它是否可以被加载程序引用(请参阅此SO 问题)
    因此,将您的 JarRunner 移动到一个包中,并在 <attribute name="Main-Class" value="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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆