如何使用终端创建 .jar 文件 [英] How to create a .jar file using the terminal
问题描述
我正在尝试使用 mac 终端创建一个 .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
那么你到底是怎么做的呢?我的 java 程序叫做 Main.java 并且我已经将它编译成一个 .class 文件.现在我该怎么办?
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?
谢谢
推荐答案
1) 确保所有必需的文件都在目录中,您已打开终端/命令提示符并导航到该目录.
1) Ensure that all necessary files are within the directory, you have opened a terminal/Command Prompt and have navigated to that directory.
2) 用
javac HelloWorld.java
3) 这将生成 JAR 文件所需的 .class 文件.
3) This will produce a .class file needed for the JAR file.
4) 接下来使用文本编辑器创建一个清单文件(使用扩展名 .txt 保存)并输入以下内容
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) 接下来使用以下代码创建 JAR 文件:
5) Next create the JAR file using this code:
jar cfm HelloWorld.jar Manifest.txt HelloWorld.class
6) 运行文件:
java -jar HelloWorld.jar
如果有任何不清楚的地方,请咨询以下网站:创建 jar 文件 和 设置应用程序入口点.
If anything seems unclear consult these websites: creating a jar file and setting an applications entry point.
希望这能帮助其他人,汤姆加油!
Hope this helps others, cheers Tom!
根据inga的评论值得注意的是,为了在jar中包含多个文件,您需要使用:
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
关注
jar cfm HelloWorld.jar Manifest.txt *.class
这篇关于如何使用终端创建 .jar 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!