如何制作可执行的 JAR 文件? [英] How to make an executable JAR file?

查看:27
本文介绍了如何制作可执行的 JAR 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由两个简单的 Java Swing 文件组成的程序.

I have a program which consists of two simple Java Swing files.

如何为我的程序制作一个可执行的 JAR 文件?

How do I make an executable JAR file for my program?

推荐答案

jar 文件只是一个包含 Java 文件集合的文件.要使 jar 文件可执行,您需要指定 main 类在 jar 文件中的位置.示例代码如下.

A jar file is simply a file containing a collection of java files. To make a jar file executable, you need to specify where the main Class is in the jar file. 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
            }
        });
    }
}

编译你的类.要制作 jar,您还需要创建一个清单文件 (MANIFEST.MF).例如,

Compile your classes. To make a jar, you also need to create a Manifest File (MANIFEST.MF). For example,

Manifest-Version: 1.0
Main-Class: JarExample

将编译后的输出类文件(JarExample.class,JarExample$1.class)和清单文件放在同一个文件夹中.在命令提示符下,转到放置文件的文件夹,然后使用 jar 命令创建 jar.例如(如果您将清单文件命名为 jexample.mf)

Place the compiled output class files (JarExample.class,JarExample$1.class) and the manifest file in the same folder. In the command prompt, go to the folder where your files placed, and create the jar using jar command. For example (if you name your manifest file as jexample.mf)

jar cfm jarexample.jar jexample.mf *.class

它将创建可执行的 jarexample.jar.

It will create executable jarexample.jar.

这篇关于如何制作可执行的 JAR 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆