How to Make executable jar file

Connect with

How to Make executable jar fileHow to make executable jar file in Java. Learn to create executable .jar Java using jar utility and Eclipse EDE.

How to execute main method in java via jar file is straightforward once you know this. This is very simple, you can build by following few steps.

1. Why You Required Executable jar?

The question is, why you required an executable .jar file in Java, answer is very simple. If you develop a small utility app or desktop application and you have to invoke the main method of a specific class, what will you do? You have to make an executable .jar file. The executable .jar file can be made either from command line or any IDE. The main method in java is the entry point of application, specially, for your desktop application or GUI based utility app.
For this type of job, you have to create a .jar file, which can used to invoke the main method , or entry of application, here, I have chosen Swing for demonstration purpose.

2. Java main method / entry point of Jar

package com.mysoftkey.swing;

import javax.swing.JFrame;
import javax.swing.JLabel;

 * This class is used to demonstration of hello world swing program.
 * and will hook this main method via executable jar file.
 * @author ranjeet jha
public class HelloWorldSwing {

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

        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("Hello World");

        //Display the window.

3. Manifest.txt file

Using a text editor i.e. notepad++ my favorite editor, created a file, say, manifest.txt containing the following lines and end with new line.

      Manifest-Version: 1.0 
      Main-Class: com.mysoftkey.swing.HelloWorldSwing

4. How to make executable jar file from command line?

Although putting all .java and .class in on single folder is not recommended but for learning purpose keep all in single folder/directory.

where HelloWorldSwing is the name of the class containing the main method you want to use.
From the command line, execute the command:

 jar -cvfm mySwingApp.jar manifest.txt com/mysoftkey/swing/*.*
jar -cvfm mySwingApp.jar manifest.txt com/mysoftkey/swing/*.class 

here, mySwingApp.jar is the jar file I’m gong to create, manifest.txt is the file that I created in step 2, and everything else is the files you want to include.

5. How to create Executable jar using Eclipse?

This is optional, either create by command line or by using eclipse, final output will be same. You can choose any one way to kae executable jar file. If you create by using Eclipse IDE , follow step-by-step as follows:

  • Select project folder and right click
  • Click Export and choose Java -> Jar Executable
  • Select Launch Configuration name
  • Provide jar location either by browse or by typing
  • Click finish and you are done with this exercise.
  • You can see executable jar file in your location that you have provided.

6. How to Run Executable Jar file?

There are two ways to execute the jar file:

  1. At the DOS or UNIX command prompt, type java -jar myResult.jar . This should work; if it doesn’t, it means, there can be of two main possibilities:
    1. ‘java’ is not recognized as an internal or external command, operable program or batch file means that you don’t have your system configured properly to run Java.
    2. Exception in thread “main” java.lang.NoClassDefFoundError: MyMainClass means that the jar file was not constructed properly. The most likely error is having something in the wrong directory when you created the jar.
  2. Double-click the jar file. This will work if your system has been configured to know about jar files. If not, see below.

7. Reference

Java Site

I hope you enjoyed this post about how to make executable jar file in java. You can visit Core Java tutorial for more example blog post.

Happy Learning! 🙂

Connect with