“Could not find the main class: XX. Program will exit.”

后端 未结 2 694
醉话见心
醉话见心 2020-12-16 15:10

I have managed to run my jar file with a command prompt, but its always giving me a reponse of

Could not find the main class: XX. Program will exit.

相关标签:
2条回答
  • 2020-12-16 15:44

    I had the same error. The problem was that Windows 10 suddenly decided to set my workspace folder to read-only.

    After removing the read-only checkmark in the folder's options, the problem was solved.

    0 讨论(0)
  • 2020-12-16 16:05

    See Setting an Application's Entry Point

    If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

    Main-Class: classname
    

    The value classname is the name of the class that is your application's entry point.

    Recall that the entry point is a class having a method with signature

     public static void main(String[] args).
    

    After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:

    java -jar JAR-name
    

    The main method of the class specified in the Main-Class header is executed.


    We first create a text file named Manifest.txt with the following contents:

    Main-Class: MyPackage.MyClass
    

    Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

    We then create a JAR file named MyJar.jar by entering the following command:

    jar cfm MyJar.jar Manifest.txt MyPackage/*.class
    

    This creates the JAR file with a manifest with the following contents:

    Manifest-Version: 1.0
    Created-By: 1.6.0 (Sun Microsystems Inc.)
    Main-Class: MyPackage.MyClass
    

    When you run the JAR file with the following command, the main method of MyClass executes:

    java -jar MyJar.jar
    
    0 讨论(0)
提交回复
热议问题