Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a .jar file using the terminal

Tags:

java

terminal

jar

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

like image 551
Katana24 Avatar asked Apr 12 '12 14:04

Katana24


People also ask

How do you create a jar file on Mac?

The standard way to create a JAR file, of course, is to use the command-line jar command included with Mac OS X's JDK. Open the Terminal, type jar , and a list of options will appear: Usage: jar {ctxu}[vfm0M] [jar-file] [manifest-file] [-C dir] files ...


5 Answers

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
like image 90
Katana24 Avatar answered Oct 25 '22 08:10

Katana24


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.
like image 42
Manoj N Revankar Avatar answered Oct 25 '22 09:10

Manoj N Revankar


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.

like image 32
user2332921 Avatar answered Oct 25 '22 07:10

user2332921


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.

like image 33
Tom J Avatar answered Oct 25 '22 08:10

Tom J


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

like image 40
mja Avatar answered Oct 25 '22 07:10

mja