How to Create an Executable Jar or Java exe file using Eclipse ide or console

We can easily create a source code or java files in our IDE like eclipse or netbeans . But what if we are  asked to create an executable jar file of the same code . We think , we are in trouble as java does not provide any class or help us with the method that can convert ant source code to executable jar file .

There are two ways to create the executable jar in java 

1. Using eclipse ide
2. Using command prompt by  using specific commands

Suppose we have awt/spring based java file i.e  JavaHungry.java .  Our aim is to convert this file from .java extension to .exe file .

We will first start with  Creating a New Runnable JAR File  using eclipse ide .

1.  Our file JavaHungry.java  will look like this in eclipse

java awt/swing file which is converted into execcutable jar eclipse





















2.   Go to the File option located top-left in the menu bar  in the eclipse

   
file in the menu of  eclipse ide


















3.  Select export option from the file menu

select export option from the file menu in eclipse ide






















4.   Expand the Java node and select Runnable JAR file. Click Next


select runnnable jar in the window eclipse ide


























5.   In the Runnable JAR File Specification page, select a 'Java Application' here "JavaHungryProject" launch configuration to use to create a runnable JAR


select the application and java class eclipse


























6.  In the  " Export destination "  field browse/choose the location where your new  executable jar file   will be created . Select an appropriate library handling method .

select the browsing location of computer eclipse






















Thats it , it will create the executable jar in the desired location .



executable jar created using eclipse ide


























The above method is using eclipse ide , now we will create an executable jar file using command prompt 
We are using the same awt/spring based java file i.e  JavaHungry.java

1.  We need to create a manifest file i.e " manifest.txt
       
       manifest.txt 

       Main-Class: JavaHungry     

Mandatory :  Manifest file should end with the new line character .

Here Main-Class is the entry gate  of the jar file (which we are going to create)  , when anyone click on the executable jar file , JavaHungry main() method will be launched .

2.  So we need to create the jar file , so we should always put manifest.txt and  JavaHungry.class together .
   
So in computer directory it will look like

C:\java\JavaHungry.class
C:\java\manifest.txt


So the following  command needs to be executed to create the executable jar of JavaHungry class file .

 jar   -cvfm  JavaHungry.jar  manifest.txt    *.class 


If you have any doubts then please express it in the comments .



About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry