Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

java - How to create a .jar file using the terminal

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...