Shiraz S Kaderuppan
Shiraz S Kaderuppan

Reputation: 68

How to create an executable Java file?

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???

Upvotes: 0

Views: 1131

Answers (3)

Shiraz S Kaderuppan
Shiraz S Kaderuppan

Reputation: 68

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)

Upvotes: 0

Anu
Anu

Reputation: 90

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

Upvotes: 2

guroosh
guroosh

Reputation: 652

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.

Upvotes: 1

Related Questions