Search code examples
javaswingjarclassnotfoundexceptionexecutable-jar

Create an executable jar for Swing app


I have created a Java application called App.java. It uses GSON. Hence I created a jar gson.jar and then created a manifest file (manifest.mf) with the following content.

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

I then created the test.jar using the following command:

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.

and run it using

java -jar test.jar

The application throws ClassNotFoundException for ExitListener. When the code that uses ExitListner is removed, it runs normal as it were run using java App. What am I missing?


Solution

  • Either Add the jar that has ExitListener class in it in your class-path

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

    or Write your own ExitListener and use in your 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);
        }
    }