14

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.

Please help me out, thanks.

bluish
  • 26,356
  • 27
  • 122
  • 180

2 Answers2

20

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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-1

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574