Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why has it failed to load main-class manifest attribute from a JAR file?

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?

like image 642
Roman Avatar asked Apr 07 '10 10:04

Roman


People also ask

How do I run a JAR file in main class?

Run a Nonexecutable JAR with Arguments We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …]


7 Answers

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
like image 123
Jon Skeet Avatar answered Oct 05 '22 14:10

Jon Skeet


  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

like image 30
weirdFactory Avatar answered Oct 05 '22 14:10

weirdFactory


You can run with:

java -cp .;app.jar package.MainClass

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

like image 35
Dainius Avatar answered Oct 05 '22 13:10

Dainius


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 
like image 26
Sridhar Sarnobat Avatar answered Oct 05 '22 14:10

Sridhar Sarnobat


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?.

like image 34
Greg Burdett Avatar answered Oct 05 '22 12:10

Greg Burdett


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

like image 30
ObviousChild Avatar answered Oct 05 '22 14:10

ObviousChild


Try

java -cp .:mail-1.4.1.jar JavaxMailHTML 

no need to have manifest file.

like image 36
user4581964 Avatar answered Oct 05 '22 13:10

user4581964