Sunday, September 17, 2023

How to Make Executable JAR file in Eclipse IDE - Java Example

If you are a Java programmer then you know what is the purpose of the JAR file, but for those who are unaware, the JAR file is deliverables of Java application. Just like C and C++ applications produce EXE files, Java produces JAR files. In other words, A JAR (Java Archive) file is a ZIP format file that bundles Java classes into a single unit, it may contain all the resources needed by Java application as well. There are mainly two types of the JAR file in Java:  Library JAR (normal JAR) files: JARs which are reusable libraries like Apache commons JAR file, guava.jar itself, or even JDBC drivers like ojdbc6_g.jar. There is another type as well, Executable JAR files: JARs which can be executed as standalone Java applications. 

The main difference between normal and executable JAR file is that later contains a manifest file, which specifies a main-class entry. When you run that JAR file, Java starts your application by reading that main-class entry, because you need the main method to execute Java programs.

I had earlier shared steps to create a JAR file from the command prompt, which if you have not read, go read it. You will learn about the basics of the JAR command, which comes with JDK, manifest file, and different attributes of the manifest file.

In this tutorial, we will learn how to make/create/export both library and executable JAR files in Eclipse IDE. Why it's important to know creating executable JAR in Eclipse because it's one of the most used tools by Java programmers.

Once you know the steps, you export your Java program as a JAR file in a second or two. After creating an executable JAR file, you can follow these steps to run the Java program from the JAR file in the command line.




Steps to Create Executable JAR file in Eclipse

A picture is worth more than a thousand words, that's why I have provided all steps by taking screenshots. You can quickly learn how to create executable JAR for your Java application in Eclipse, by just looking at these screenshots. 

They are arranged from start to end. This means the first image is for the first step and the last image is for the last step. 

By the way, if your application is dependent upon some other JAR files e.g. third-party library, then don't need to include them inside JAR. All you need to do is include them inside your Java Classpath. Java is smart enough to pick classes from those JAR. 

Also remember to include the main method or entry point of your Java program, while creating executable JAR, as you can not run a Java program without the main method, as discussed here.

Step 1

Select your project and click on the Export option.


How to Make Executable JAR file in Eclipse IDE - Java




Step 2

Choose Java and select JAR file for exporting




Step 3

Now select the source and resources you want to export into a JAR file. After selecting the src folder and any resource, select the destination folder, where you want your JAR file to be created. Also, don't forget to check the option "compress the content of JAR file"

This will reduce the size of the executable JAR file. If you want you can finish the process in this step, but you can also go one step further to save these instructions of executable JAR file creation for future use.




Step 4

In this step, you can save all instructions into a "JAR description" file for future use. This will create a description file e.g. Test.jardesc in the chosen folder.




Step 5

This is an important step because you are going to generate the manifest file, don't forget to choose the main class. This is important to make your JAR an executable JAR file. Remember the main method is the entry point of any Java application.




Step 6

You are done with creating an executable JAR file. Now you can go to the target folder, which you have chosen in previous steps to see both the JAR file as well .jardesc file to recreate the JAR file again and again. This time you don't need to include main class as well, as those are already included.




Step 7

After making code changes, if you want to create a new JAR file, you don't need to go through all previous steps. This time, just select your JAR description file and say "create JAR", a right click menu option in Eclipse. This will create another JAR file in same folder. You can double check timestamp of JAR file.



That's all about How to create or make an Executable JAR file in Eclipse IDE. By following these steps you can export your Java program as executable JAR, which allows you to share your program with your user, client, and anyone who wants to take a look. I strongly suggest saving instructions to export the JAR file, so that you don't need to run through all these steps again and again. 

Next time, when you update your code, just click on your jardesc file and your JAR file will be ready in a blink.
Lastly, one question for you, What is your favorite IDE? Eclipse, IntelliJIDEA, NetBeans, VS Code, or anything else?

21 comments:

  1. love this blog so much.. thank you javin paul for beng a blessing to me.. I would love to see more on data structures and algorithms in java.. thanks alot

    ReplyDelete
    Replies
    1. Thank You Anonymous. I will surely write some more tutorials and interview question experience on data structures and algorithms in Java. Stay tuned.

      Delete
  2. hey admin hi.., m just a beginner in Java, i just want to code a java transliteration programme (from English to Hindi).This is my project task and i really don't know where to start from.Please help me.

    ReplyDelete
  3. Finally I found, very very thanks

    ReplyDelete
  4. I tried to doing the Same but i am facing a problem , IN Java virtual machine launcher "A Java Exception has occurred" message is displayed.
    Admin can u let me know a way to resolve it

    ReplyDelete
  5. Hello, This is not working with JUnit Test Suites. Please help to add all packages in JAR.

    ReplyDelete
    Replies
    1. If your project is depending upon other JAR file e.g. JUnit (your real code should not depend upon JUnit ) then you also need to add those JAR while exporting. Manifest file contains entries for them.

      Delete
  6. I follow but there is error "jar creation failed", show details: "Exported with compile warning...". Can you show me how to fix this problem.

    ReplyDelete
    Replies
    1. It could be due to compilation error in your project, just make sure you project compile and run fine before you make executable JAR file.

      Delete
  7. Hi,

    Thank you for your help but... I´m facing an annoying problem here, that I don´t know how to solve.

    Well... I´m new in Java, so.. be pacient.. =D

    I´ve been followed all those steps but when I try to run the .jar file by "double click", the following message is showed at the prompt:

    "Error: Could not find or load main class"

    AND when I try to load via command line, well... that´s the return:

    java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at application.Main.start(Main.java:15)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163
    (Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown
    Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Sourc
    e)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown S
    ource)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    Can you help me please?

    My project loads a simple FXML file.

    ReplyDelete
  8. i created jar file as per your screenshot but how to execute that jar file

    ReplyDelete
  9. Your site helped me a lot.Step by step explanation with appropriate and clear screenshots..Great Job.

    ReplyDelete
  10. Hello, I went through the process and the jar file was created, but it does not work when I click on it.

    ReplyDelete
  11. Hello...please also share the steps to create Library JAR files in Eclipse

    ReplyDelete
    Replies
    1. Hello Rashmi, just follow the same steps but don't select any Main class.

      Delete
  12. Just wanted to say that this is the only thing that worked for me. Thank you so much for your thorough explanation!

    ReplyDelete
  13. Thanks, this worked for me on linux mint

    ReplyDelete
  14. Hello there :)
    I tried following a few similar tutorials but they just don't work. I have made a game in eclipse that has some photos as backgrounds and images as buttons in the game. When I start game in eclipse it works just fine. When I tried to export it as JAR file and then convert it to exe file using Launch4j frame appears blank, only my timer of the game is seen in the corner. When I tried this method I selected all of those additional things I used (images etc.) for my JAR file in step 3 of this tutorial, but it still doesn't work. Do you have any idea what might be the problem and how to fix it?

    ReplyDelete
  15. Please help me in getting the solution for a query "How to import executable jar file into an existing project in eclipse?"

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.