简体   繁体   English

如何制作可执行的 JAR 文件?

[英]How to make an executable JAR file?

I have a program which consists of two simple Java Swing files.我有一个由两个简单的 Java Swing 文件组成的程序。

How do I make an executable JAR file for my program?如何为我的程序制作可执行的 JAR 文件?

A jar file is simply a file containing a collection of java files. jar 文件只是一个包含一组 java 文件的文件。 To make a jar file executable, you need to specify where the main Class is in the jar file.要使 jar 文件可执行,您需要指定main Class 在 jar 文件中的位置。 Example code would be as follows.示例代码如下。

public class JarExample {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // your logic here
            }
        });
    }
}

Compile your classes.编译你的类。 To make a jar, you also need to create a Manifest File ( MANIFEST.MF ).要制作 jar,您还需要创建一个清单文件 ( MANIFEST.MF )。 For example,例如,

Manifest-Version: 1.0
Main-Class: JarExample

Place the compiled output class files (JarExample.class,JarExample$1.class) and the manifest file in the same folder.将编译后的输出类文件(JarExample.class,JarExample$1.class)和清单文件放在同一个文件夹中。 In the command prompt, go to the folder where your files placed, and create the jar using jar command.在命令提示符下,转到放置文件的文件夹,然后使用 jar 命令创建 jar。 For example (if you name your manifest file as jexample.mf)例如(如果您将清单文件命名为 jexample.mf)

jar cfm jarexample.jar jexample.mf *.class

It will create executable jarexample.jar.它将创建可执行的 jarexample.jar。

In Eclipse you can do it simply as follows :Eclipse 中,您可以简单地执行以下操作:

Right click on your Java Project and select Export .右键单击您的 Java 项目并选择Export

Select Java -> Runnable JAR file -> Next.选择Java -> Runnable JAR 文件-> 下一步。

Select the Launch Configuration and choose project file as your Main class选择 Launch Configuration 并选择项目文件作为您的Main 类

Select the Destination folder where you would like to save it and click Finish.选择要保存的目标文件夹,然后单击完成。

Here it is in one line:这是在一行中:

jar cvfe myjar.jar package.MainClass *.class

where MainClass is the class with your main method, and package is MainClass 's package.其中MainClass是您的main方法的类, packageMainClass的包。

Note you have to compile your .java files to .class files before doing this.请注意,在执行此操作之前,您必须将.java文件编译为.class文件。

c  create new archive
v  generate verbose output on standard output
f  specify archive file name
e  specify application entry point for stand-alone application bundled into an executable jar file

This answer inspired by Powerslave's comment on another answer.这个答案的灵感来自 Powerslave 对另一个答案的评论。

If you use maven, add the following to your pom.xml file:如果您使用 maven,请将以下内容添加到您的pom.xml文件中:

<plugin>
    <!-- Build an executable JAR -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.path.to.YourMainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

Then you can run mvn package .然后你可以运行mvn package The jar file will be located under in the target directory. jar 文件将位于目标目录下。

It's too late to answer for this question.回答这个问题为时已晚。 But if someone is searching for this answer now I've made it to run with no errors.但是如果现在有人正在寻找这个答案,我已经让它运行起来没有错误。

First of all make sure to download and add maven to path .首先确保下载并添加maven 到 path [ mvn --version ] will give you version specifications of it if you have added to the path correctly. [ mvn --version ] 如果您已正确添加到路径中,则会为您提供它的版本规范。

Now , add following code to the maven project [ pom.xml ] , in the following code replace with your own main file entry point for eg [ com.example.test.Test ].现在,将以下代码添加到maven 项目[ pom.xml ],在以下代码中替换为您自己的主文件入口点,例如 [ com.example.test.Test ]。

      <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                    <mainClass>
            your_package_to_class_that_contains_main_file .MainFileName</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

Now go to the command line [ CMD ] in your project and type mvn package and it will generate a jar file as something like ProjectName-0.0.1-SNAPSHOT.jar under target directory.现在转到项目中的命令行 [ CMD ] 并键入mvn package ,它将在target目录下生成一个类似于ProjectName-0.0.1-SNAPSHOT.jar的 jar 文件。

Now navigate to the target directory by cd target .现在通过cd target导航到目标目录

Finally type java -jar jar-file-name.jar and yes this should work successfully if you don't have any errors in your program.最后输入java -jar jar-file-name.jar是的,如果您的程序中没有任何错误,这应该会成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM