简体   繁体   English

为Swing应用创建可执行jar

[英]Create an executable jar for Swing app

I have created a Java application called App.java . 我创建了一个名为App.java的Java应用程序。 It uses GSON. 它使用GSON。 Hence I created a jar gson.jar and then created a manifest file ( manifest.mf ) with the following content. 因此,我创建了一个jar gson.jar ,然后创建了一个清单文件( manifest.mf ),内容如下。

Manifest-Version: 1.0
Class-Path: test.jar gson.jar
Main-Class: App

I then created the test.jar using the following command: 然后,我使用以下命令创建了test.jar:

jar cfm test.jar manifest.mf *

Edit: jar cfm test.jar manifest.mf * is producing the jar but nowhere to be seen on the disk. 编辑: jar cfm test.jar manifest.mf * 正在生成jar,但磁盘上无处可见。

and run it using 并使用运行

java -jar test.jar

The application throws ClassNotFoundException for ExitListener . 应用程序为ExitListener抛出ClassNotFoundException When the code that uses ExitListner is removed, it runs normal as it were run using java App. 删除使用ExitListner的代码后,它与使用Java App运行时一样正常运行。 What am I missing? 我想念什么?

Either Add the jar that has ExitListener class in it in your class-path 在您的类路径中添加具有ExitListener类的jar

Class-Path: test.jar gson.jar <exitlistenerjar>

or Write your own ExitListener and use in your App. 或编写自己的ExitListener并在您的App中使用。

EDIT 编辑

On other thought you can use this code and add it to your application 换个角度,您可以使用此代码并将其添加到您的应用程序中

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ExitListener extends WindowAdapter {
    public void windowClosing(WindowEvent event) {
        System.exit(0);
    }
}

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

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