问题描述
我正在学习Java,但我遇到了问题。我创建了6个不同的类,每个类都有自己的 main()
方法。我想为每个类创建可执行文件 .jar
,即6个可执行文件 .jar
文件。
I'm learning Java and I have a problem. I created 6 different classes, each has it's own main()
method. I want to create executable .jar
for each class, that is 6 executable .jar
files.
到目前为止,我试过
java -jar cf myJar.jar myClass.class
我得'无法访问jarfile cf'。我做错了什么,但我不知道是什么。我也使用Eclipse IDE,如果这意味着什么。
and I get 'Unable to access jarfile cf'. I'm doing something wrong but I don't know what. I'm also using Eclipse IDE if that means something.
推荐答案
为了创建.jar文件,你需要使用 jar
而不是 java
:
In order to create a .jar file, you need to use jar
instead of java
:
jar cf myJar.jar myClass.class
此外,如果你想做它可执行文件,您需要为您的应用程序指明入口点(即,带有 public static void main(String [] args)
的类)。这通常是通过创建包含清单文件来完成的。 code> Main-Class 标题(例如, Main-Class:myClass
)。
Additionally, if you want to make it executable, you need to indicate an entry point (i.e., a class with public static void main(String[] args)
) for your application. This is usually accomplished by creating a manifest file that contains the Main-Class
header (e.g., Main-Class: myClass
).
但是,正如Mark Peters指出的那样,使用JDK 6,您可以使用 e
选项来定义条目point:
However, as Mark Peters pointed out, with JDK 6, you can use the e
option to define the entry point:
jar cfe myJar.jar myClass myClass.class
最后,你可以执行它:
java -jar myJar.jar
参见
- Creating a JAR File
- Setting an Application's Entry Point with the JAR Tool
See also
这篇关于Java创建.jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!