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

查看:120
本文介绍了如何制作可执行的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 Class在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天全站免登陆