86

I have created a JAR file in this way jar cf jar-file input-files. Now, I'm trying to run it. Running it does not work (jre command is not found):

jre -cp app.jar MainClass

This does not work either:

java -jar main.jar

(Failed to load Main-Class manifest attribute from main.jar).

I also found out that

To run an application packaged as a JAR file (version 1.2 -- requires Main-Class manifest header)

What is the "Main-Class manifest header"? How do I create it and where do I put it?

Eric Reed
  • 377
  • 1
  • 6
  • 21
Roman
  • 124,451
  • 167
  • 349
  • 456

11 Answers11

62

I'm not sure I believe your symptoms:

  • If the jre command isn't found, then running jre -cp app.jar should give the same error
  • Just adding a JAR file to the classpath shouldn't give the error you're seeing

I'd expect you to see this error if you run:

java -jar app.jar

The Main-Class header needs to be in the manifest for the JAR file - this is metadata about things like other required libraries. See the Sun documentation for how to create an appropriate manifest. Basically you need to create a text file which includes a line like this:

Main-Class: MainClass

Then run

jar cfm app.jar manifest.txt *.class
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you. It works. I created a manifest file with the `Main-Class: MainClass` and then created the .jar file with the command you gave. After that I was able to run my program through `java -jar main.jar`. – Roman Apr 07 '10 at 11:36
  • 1
    Another option is using the -e option, without needing a separate file. `jar cfe app.jar MainClass *.class` Note: -e was added in JDK 6. – pferate Nov 07 '13 at 03:36
35
  1. set the classpath and compile

    javac -classpath "C:\Program Files\Java\jdk1.6.0_updateVersion\tools.jar" yourApp.java

  2. create manifest.txt

    Main-Class: yourApp newline

  3. create yourApp.jar

    jar cvf0m yourApp.jar manifest.txt yourApp.class

  4. run yourApp.jar

    java -jar yourApp.jar

Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
weirdFactory
  • 561
  • 5
  • 4
9

You can run with:

java -cp .;app.jar package.MainClass

It works for me if there is no manifest in the JAR file.

Dainius
  • 1,765
  • 1
  • 17
  • 36
  • I've: Unrecognized option: -cp. Could you review your answer? Because I don't what this command is trying to do. (java -cp.;app.jar package.MainClass). This is one command, or two? – kenorb Aug 29 '12 at 20:40
  • 1
    http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html "-cp.;app.jar" should add your app.jar file to class path, than you can run MainClass.main() method (MainClass is in "package" package). – Dainius Aug 30 '12 at 08:53
  • 1
    So probably on Linux this option (-cp) doesn't exist (mine is java v1.6.0_24, OpenJDK Runtime Environment (IcedTea6 1.11.3) (6b24-1.11.3-1ubuntu0.12.04.1)). – kenorb Sep 05 '12 at 11:28
  • java -version => Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode) "java -cp . test1.Main" works – Dainius Sep 06 '12 at 09:34
  • /usr/lib/jvm/java-6-openjdk-amd64/bin/java -version => OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode) "/usr/lib/jvm/java-6-openjdk-amd64/bin/java -cp . test1.Main" works – Dainius Sep 06 '12 at 09:36
  • java -h => -cp – Dainius Sep 06 '12 at 09:36
  • och, there's need space between "-cp" and dot – Dainius Sep 06 '12 at 09:37
6

I got this error, and it was because I had the arguments in the wrong order:

CORRECT

java maui.main.Examples tagging -jar maui-1.0.jar 

WRONG

java -jar maui-1.0.jar maui.main.Examples tagging 
Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
1

I was getting the same error when i ran:
jar cvfm test.jar Test.class Manifest.txt

What resolved it was this:
jar cvfm test.jar Manifest.txt Test.class

My manifest has the entry point as given in oracle docs (make sure there is a new line character at the end of the file):
Main-Class: Test

ObviousChild
  • 119
  • 1
  • 9
1

Try

java -cp .:mail-1.4.1.jar JavaxMailHTML 

no need to have manifest file.

Ram
  • 3,092
  • 10
  • 40
  • 56
1

The easiest way to be sure that you have created the runnable JAR file correctly, with the appropriate manifest file, is to use Eclipse to build it for you. In your Eclipse project, you basically just select File/Export from the menu, and follow the prompts.

That way, you can be sure that your JAR file is correct and will know to look elsewhere if there is still an issue. The process is described in full in FAQ How do I create an executable JAR file for a stand-alone SWT program?.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Greg Burdett
  • 191
  • 2
  • 6
0

I discovered that I was also having this error in NetBeans. I hope the following is helpful.

  1. Make sure that when you go to Project Configuration you set the main class you intend for running.
  2. Do a Build or Clean Build
  3. Place the jar file where you wish and try: java -jar "YourProject.jar" again at the command line.

This was the problem I was getting because I had other "test" programs I was using in NetBeans and I had to make sure the Main Class under the Run portion of the Project configuration was set correctly.

many blessings, John P

john p
  • 132
  • 1
  • 4
0

I faced the same problem. This unix command is not able to find the main class. This is because the runtime and compile time JDK versions are different. Make the jar through eclipse after changing the java compiler version. The following link helped me.

http://crunchify.com/exception-in-thread-main-java-lang-unsupportedclassversionerror-comcrunchifymain-unsupported-major-minor-version-51-0/

Try running the jar created after this step and then execute it

Harshita Sethi
  • 2,035
  • 3
  • 24
  • 46
0

If your class path is fully specified in manifest, maybe you need the last version of java runtime environment. My problem fixed when i reinstalled the jre 8.

AmirHossein Rezaei
  • 1,086
  • 1
  • 16
  • 20
0

If you using eclipse, try below: 1. Right click on the project -> select Export 2. Select Runnable Jar file in the select an export destination 3. Enter jar's name and Select "Package required ... " (second radio button) -> Finish

Hope this helps...!

Barani r
  • 2,119
  • 1
  • 25
  • 24