Benjamin
Benjamin

Reputation: 1104

Executable jar file doesn't execute

To preface this : I am not very experienced with Java. I wanted to write a window application based on Swing. Creating and executing the class file works but after creating the jar file the file doesn't execute when double clicking it. I'm working with SE 1.8.0_131 on Windows 10 (64 bit).

My steps where as follows:

I have created this test file

import java.awt.event.*; 
import javax.swing.*;

public class SwingTest {

    public static void main(String[] args){

        JFrame mainWindow = getMainWindow();

        mainWindow.setVisible(true);
    }

    public static JFrame getMainWindow(){
        JFrame frame = new JFrame("Hauptfenster");

        // Initialize Window
        frame.setLayout(null);
        frame.setSize(300, 300);

        // Close application on window close
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // Create and add Close-button
        JButton exitButton = new JButton("Close");
        exitButton.addActionListener(ae -> System.exit (0));
        exitButton.setLocation(20, 20);
        exitButton.setSize(100, 40);
        frame.add(exitButton);

        return frame;
    }
}

Then I run this command to create the class file:

javac SwingTest.java

This successfully created the class file and I could execute it with the following command:

java SwingTest

I created a manifest file named SwingTest.mf

Manifest-Version: 1.0
Main-Class: SwingTest

Then I run the following command to create the jar file (I wrote this command based on this stackoverflow post)

jar cfm SwingTest.jar SwingTest.mf *.class

This created the jar file but when I double click the file nothing happens (no window or CLI pops or flickers up, no process appears in the tasks)

What did I miss or do wrong to create the executable?

Sorry for the mass of details and thanks in advance

Upvotes: 0

Views: 1421

Answers (3)

Benjamin
Benjamin

Reputation: 1104

Thanks for all the suggestions. I found the problem.

After reading through this page I noticed that my manifest file misses a new line at the end. Therefore my class statement was never added to the manifest which was created in the jar file. After adding the new line at the end of my Swingtest.mf and running the following command it finally produced a working jar file

jar cfm SwingTest.jar SwingTest.mf SwingTest.class

Upvotes: 1

Mikita Berazouski
Mikita Berazouski

Reputation: 1001

  1. Create new folder and place there SwingTest.java
  2. Then compile your class javac SwingTest.java
  3. Create folder META-INF and MANIFEST.MF file there with following content

    Manifest-Version: 1.0
    Main-Class: SwingTest

  4. Run following command jar cfm SwingTest.jar META-INF/MANIFEST.MF *.class

  5. Double click on SwingTest.jar and enjoy.

enter image description here

Upvotes: 0

mentallurg
mentallurg

Reputation: 5207

It seems that "jar" extension is associated with some other tool like 7-zip. Do following: In the Explorer press the right mouse button, in the context menu select "Open with...". There select of navigate to java.exe or better javaw.exe. Mark the check box to always use this association. Then next time when you you double click the Java will be called and your jar will be executed.

Upvotes: 0

Related Questions