Could not find or load main class with a Jar File

后端 未结 16 1063
有刺的猬
有刺的猬 2020-12-01 09:09

I\'m trying to load a jar using

@echo off
java -jar Test.jar
pause

With the manifest of

Manifest-Version: 1.0
Main-Class:          


        
相关标签:
16条回答
  • 2020-12-01 09:19

    I was getting this error because my main class is in test package, and I am creating artifact using IntelliJ. After I checked the box Include Tests when creating artifact, it got resolved.

    Screenshot from IntelliJ showing Include Tests checkbox

    0 讨论(0)
  • 2020-12-01 09:21

    1.Create a text file calles Manifest.txt and provide the value as

    Main-Class: classes.TestClass

    2.Create the jar as

    jar cfm test.jar Manifest.txt classes/*.class

    3.Run the jar as

    java -jar test.jar

    0 讨论(0)
  • 2020-12-01 09:23

    I follow the following instruction to create a executable .jar in Eclipse. Then Run command "java -jar .jar " to launch the program.

    It takes care of creating mainfest and includeing main class and library files parts for you.

    http://java67.blogspot.com/2014/04/how-to-make-executable-jar-file-in-Java-Eclipse.html

    0 讨论(0)
  • 2020-12-01 09:23

    in IntelliJ, I get this error when trying to add the lib folder from the main project folder to an artifact.

    Placing and using the lib folder in the src folder works.

    0 讨论(0)
  • 2020-12-01 09:26

    At least the way I've done this is as follows:

    If you have a nested src tree (say com.test.myclass.MyClass) and you are compiling from a root directory you need to do the following:

    1) when you create the jar (usually put this in a script): jar -cvfm my.jar com/test/myclass/manifest.txt com/test/myclass/MyClass.class

    2) The manifest should look like:

    Mainfest-version: 1.0 Main-Class: com.test.myclass.MyClass Class-Path: . my.jar

    3) Now you can run the jar from anywhere like this:

    java -jar my.jar

    Hope this helps someone

    0 讨论(0)
  • 2020-12-01 09:28

    I had this error because I extracted JAR files from libraries to the target JAR by IntellijIDEA. When I choose another option "copy to the output directory...", it solved the problem. Hope this helps you

    0 讨论(0)
提交回复
热议问题