0

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

Benjamin
  • 1,067
  • 20
  • 30
  • 2
    Is your manifest file included **within** the jar itself? When you try to run the jar file from the command line, what happens? – Hovercraft Full Of Eels Jun 03 '18 at 19:51
  • @HovercraftFullOfEels When I run the jar on the CLI with java -jar SwingTest.jar it does say "no main manifest attribute”. When I open the jar in 7-ZIP I see a folder "META-INF" and a file SwingTest.class. The folder contains a file "MANIFEST.MF". The file didn't contain Main-Class: SwingTest so I added it but even afterwards nothing happens when double clicking the jar. – Benjamin Jun 03 '18 at 19:58
  • Benjamin: Please show the full directory tree of the jar file. Also, do you use packages? if so you're not using the correct full name to the main class in the manifest as that should include the full name of the class, which includes the packages. – Hovercraft Full Of Eels Jun 03 '18 at 20:00
  • Note: with newer versions of jar there is a -e switch that lets you specify a Main-Class on the command line without needing to create your own manifest file. But I followed the steps in this question and it worked fine on Windows 10 with JDK 11-ea+16. – David Conrad Jun 03 '18 at 20:17
  • I would examine the jar with `unzip -Z -1 SwingTest.jar` and `unzip -p SwingTest.jar META-INF/MANIFEST.MF` to ensure it contains what I expect it to contain. – David Conrad Jun 03 '18 at 20:19
  • found the problem. I need to add a new line at the end of my manifest file. After adding it the resulting jar file works. – Benjamin Jun 03 '18 at 20:36

3 Answers3

1

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
Benjamin
  • 1,067
  • 20
  • 30
0

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.

mentallurg
  • 4,967
  • 5
  • 28
  • 36
  • Well I used 7-ZIP after the problems occured to check the jar contents. It is the first time I ever created a jar file. Also wouldn't 7-ZIP pop up when double clicking the jar if it was associated with that extension? – Benjamin Jun 03 '18 at 20:05
  • 1
    And I just checked: it is associated with "Java(TM) Platform SE binary" – Benjamin Jun 03 '18 at 20:05
0
  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