Java. Jar when running Java – jar lang.ClassNotFoundException

I'm using ant to build my build XML file, which is compiled, but when the generated jar is run through "Java - jar my_jar. Jar", the runtime Java. Jar is obtained lang.NoClassDefFoundError. There seems to be a lot of problems, but no solutions to related problems are useful to me

My javac classpath only contains "/ usr / local / lib / libthrift. Jar", the main Java files import a bunch of thrift packages, such as org apache. thrift. transport. TTransportException.

When I try to run the program by:

java -jar MyClass.jar

, I received an 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.

This is something I've tried so far that doesn't work:

>Add a flag on the command line, such as "Java - CP / usr / local / lib / libthrift.jar - jar my_jar. Jar". The result is the same as the above error > Add & lt; Attribute name = "class path" value = ". /: / usr / local / lib / libthrift. Jar" / > in the list of my jar > tag, the result is the same as the above error > Set - xbootclasspath / A: / usr / local / lib / libthrift jar:./ Add to Java command line It solves the first error, but a different error occurs:

Exception in thread "main" Java Lang.noclassdeffounderror: org / Apache / log4j / logger apache. thrift. transport. TServerSocket.< Clinit > (tserversocket. Java: 36) in MyClass Start (unknown source) in MyClass Main (unknown source)

Edit:

If I comment out the missing class but keep the imported code, the code will execute normally

Edit:

I moved my java class to the server and referenced mainclass with the server in the manifest attribute, but this did not fix any problems

Solution

Could not find the main class: MyClass
Could not find the main class: MyClass

The error appears to be related to your manifest:

>There may not be a complete classpath class path: see this howto

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

>Or maybe not in 'my'_ jar. Mainclass is fully defined in '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>

< attribute name = "main class" value = "howto. Hello" / > you need to specify the full path (package) of mainclass, not just mainclass

If your main class is in the default package, I'm not sure whether the loader can reference it (see so question). Therefore, move your jarrunner to a package and declare it in < attribute name = "main class" value = "mypackage. Jarrunner" / > Components

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>