25

I'm trying to create a .jar file using the mac terminal but am having bit of trouble. I followed the steps here but was unsuccessful. I got this error message:

Failed to load Main-Class manifest attribute from
aclient.jar

So how exactly do you go about doing it? My java program is called Main.java and I have compiled it into a .class file. Now what do I do?

Thanks

progyammer
  • 1,498
  • 3
  • 17
  • 29
Katana24
  • 8,706
  • 19
  • 76
  • 118

5 Answers5

58

1) Ensure that all necessary files are within the directory, you have opened a terminal/Command Prompt and have navigated to that directory.

2) Compile the .java class, for example HelloWorld.java with

javac HelloWorld.java

3) This will produce a .class file needed for the JAR file.

4) Next create a manifest file (saved using the extension .txt) using the text editor and input the following

Main-Class: HelloWorld

or whatever your file's name is.

5) Next create the JAR file using this code:

jar cfm HelloWorld.jar Manifest.txt HelloWorld.class

6) Run the file:

java -jar HelloWorld.jar

If anything seems unclear consult these websites: creating a jar file and setting an applications entry point.

Hope this helps others, cheers Tom!

Edit:

Following inga's comment it's worth noting that in order to include multiple files in the jar you need to use the:

javac *.java

followed by

jar cfm HelloWorld.jar Manifest.txt *.class
Katana24
  • 8,706
  • 19
  • 76
  • 118
  • 1
    Thank you! I got into some trouble following your instructions because I have multiple java files in my project. At first I thought I only needed to compile my main class but if you have multiple java files, you have to compile them all so instead of "javac HelloWorld.java" you would have to do "javac *.java" and "jar cfm HelloWorld.jar Manifest.txt HelloWorld.class" would be "jar cfm HelloWorld.jar Manifest.txt *.class". Still you only have to state the main class in the Manifest file. Hope this helps if someone's doing the same mistake I did. – inga Oct 27 '15 at 21:31
  • 1
    @inga I've updated my answer to include your comments. Thanks for the information! – Katana24 Oct 28 '15 at 14:33
  • The manifest also must end with a newline, otherwise it will be disregarded silently. And the error "no main manifest attribute, in *.jar" will be called. I'm adding this as a comment, because it can be very tricky to debug; since everything can be correct code in any other aspect. – StiggyStardust Apr 14 '23 at 17:33
8

Yes, we need to use new line at the end of class name... It worked for me

i.e. Main-Class: HelloWorld

 It will look like this in Notepad++
 1.Main-Class: HelloWorld
 2.
3

Maybe this will help re Manifest.txt file:

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.

user2332921
  • 369
  • 1
  • 3
  • 7
2

You need to have an text file that defines the main class. E.g.

% cat MyMain
Main-Class: HelloWorld

then

%jar cvfm foo.jar MyMain *.class
%java -jar foo.jar
Hello world 

See: http://java.sun.com/j2se/1.4.2/runtime.html#example.

fospathi
  • 537
  • 1
  • 6
  • 7
Tom J
  • 101
  • 1
  • I followed your link and created a simple example. For the text file is the contents just Main-Class: HelloWorld Or should there be some other text in there. Also should it be saved as a .txt file or with .mf or something? – Katana24 Apr 12 '12 at 16:35
  • 1
    Btw I'm still getting Failed to loa Main-Class manifest error even after following your advice :/ – Katana24 Apr 12 '12 at 16:38
  • Try % jar xvf foo.jar created: META-INF/ inflated: META-INF/MANIFEST.MF inflated: HelloWorld.class and then look at the MANIFESTMF it should look like Manifest-Version: 1.0 Created-By: 1.6.0_31 (Apple Inc.) Main-Class: HelloWorld – Tom J Apr 12 '12 at 17:41
  • I got it working now - I followed the advice on the Sun website here http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html under the An Example part, though I don't know why it didn't work based on what you outlined – Katana24 Apr 12 '12 at 22:17
2

I followed the instructions and was getting issue while loading the Manifest.txt file.

In Manifest.txt we just simply need to write Main-class: class_name

But after above statement please press enter in Manifest.txt and your Manifest.txt load issue will be resolved. New line at the end of Manifest file is required.

Regards

mja
  • 31
  • 3