views:

5626

answers:

3

I have created a jar file in this way jar cf jar-file input-files. Now I try to run it. This does not work: jre -cp app.jar MainClass (jre command is not found). This java -jar main.jar also does not work (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?

ADDED I made a mistake in my original question. The second command I tried was: java -jar main.jar.

+6  A: 

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
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
A: 

Take a look at this tutorial -> http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html

Oliver Michels
A: 

You can run with java -cp.;app.jar package.MainClass It works for me if in jar file is no manifest.

Dainius