0

Although this question might have already been asked numerous times in SO, I'm unable to create an executable Java file (*.jar).

I have tried closely following the instructions in the highest-voted solution at How do I create executable Java program?, but when I double-clicked on the jar file, nothing happened (no pop-up window appeared). Please advise on what could be wrong here.

Thanks.

============UPDATE===================

For those who wish to know, here's the source code I used:

HelloWorldSwing.java

package start;

    import javax.swing.*;

    public class HelloWorldSwing {
        public static void main(String[] args) {
            //Create and set up the window.
            JFrame frame = new JFrame("HelloWorldSwing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JLabel label = new JLabel("Hello World");
            frame.add(label);

            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    }
    class Dummy {
        // just to have another thing to pack in the jar
    }

Manifest.mf

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.8.0_172 (Oracle Corporation)
Main-Class: HelloWorldSwing

=========================================

Now, I've tried it with another *.java file, and the same problem recurs! Please see the code below for details:

Code:

SwingExample.java

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

public class SwingExample implements Runnable {

    @Override
    public void run() {
        // Create the window
        JFrame f = new JFrame("Hello, !");
        // Sets the behavior for when the window is closed
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // Add a layout manager so that the button is not placed on top of the label
        f.setLayout(new FlowLayout());
        // Add a label and a button
        f.add(new JLabel("Hello, world!"));
        f.add(new JButton("Press me!"));
        // Arrange the components inside the window
        f.pack();
        // By default, the window is not visible. Make it visible.
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingExample se = new SwingExample();
        // Schedules the application to be run at the correct time in the event queue.
        SwingUtilities.invokeLater(se);
    }

}

Manifest.mf

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.8.0_172 (Oracle Corporation)
Main-class: start.SwingExample

WTH is happening here???

  • You need show your source code,if it's a GUI application,then there will be pop-up window,otherwise ,you need run it via cmd like `java -jar test.jar` – flyingfox Jun 29 '18 at 03:59
  • Hi lucumt, My source code is exactly the same as that provided by OscarRYZ at https://stackoverflow.com/questions/804466/how-do-i-create-executable-java-program (I followed all of his instructions & code to the letter). However, when I double-click on the jar file generated (hello.jar), nothing happens? – Shiraz S Kaderuppan Jun 29 '18 at 04:06
  • @lucumt: Also, when I try to run java -jar hello.jar, I get the following error: Error: Could not find or load main class HelloWorldSwing Caused by: java.lang.NoClassDefFoundError: start/HelloWorldSwing (wrong name: HelloWorldSwing) – Shiraz S Kaderuppan Jun 29 '18 at 04:15

3 Answers3

2

Creating a jar File in Command Prompt

Start Command Prompt.Navigate to the folder that holds your class files:

  1. C:>cd \mywork

    Set path to include JDK’s bin. For example:

    C:\mywork> path c:\Program Files\Java\jdk1.7.0_25\bin;%path%

  2. Compile your class(es):

    C:\mywork> javac *.java

  3. Create a manifest file and your jar file:

    C:\mywork> echo Main-Class: Craps >manifest.txt

    C:\mywork> jar cvfm Craps.jar manifest.txt *.class

    or

    C:\mywork> jar cvfe Craps.jar Craps *.class

  4. Test your jar:

    C:\mywork> Craps.jar

    or

    C:\mywork> java -jar Craps.jar

Reference link: http://www.skylit.com/javamethods/faqs/createjar.html

Anu
  • 80
  • 1
  • 2
  • 13
1

If your jar file is created successfully then try opening it using some extractor to see the class files. Use archive manager for Ubuntu and winRar for windows.

guroosh
  • 642
  • 6
  • 18
  • Hi Guroosh, Thanks for your reply. I have tried opening the jar file using 7Zip (treating it as a normal archive), and I see 2 folders (start & META-INF). The start folder contains Dummy.class & HelloWorldSwing.class; while the META-INF folder contains the MANIFEST.MF file. When I open MANIFEST.MF, it contains the following line of code: Main-Class: HelloWorldSwing. Thanks. – Shiraz S Kaderuppan Jun 29 '18 at 04:07
0

OK, seems like the issue is resolved when I changed Manifest.mf to read Main-Class: start.HelloWorldSwing. Evidently, this was also indicated by OscarRyz in his solution at How do I create executable Java program?, but when I tried it the first time, it did not seem to work correctly.

Hmm...

============UPDATE================

Based on further testing, it seems that the following line of code works bettter than the compilation procedure suggested by OscarRyz:

jar cvfm SwingExample.jar manifest.txt *.class

(Reference: www.skylit.com/javamethods/faqs/createjar.html)