Have a .java file/program that you would like to run outside of JAVA’s environment, change it from java to exe? You have come to the right place!
This is the complete guide to convert a JAVA file to JAR file and then convert the JAR file to EXE file which will result your JAVA program to become executable.
It is a simple follow along guide, you do not require any knowledge on the programs used here. Two main programs used are the in-built command prompt and Launch4j. The only requirement is patience.
For this case scenario, I have created a simple GUI in JAVA which display a pop-up box with “Hello World” written in it. Let’s start!
Create Java file/ Locate Java file
Step 1: First of all, Locate your java file you would like to convert to an executable file.
Step 2: Open command prompt (windows key + r, type “cmd”, press “ok”)
Step 3: Locate to the folder you have your java file using “cd” command.
Step 4: Let’s compile your java file to create a class file. Type “javac yourfilename.java”.
Check if your file is working with “java yourfilename.java”.
Convert JAVA file to JAR file
Step 5: Secondly, lets convert your whole file to a JAR file. JAR file is a java archive file that contains all your data and resources used in your java file as a single archive file. We need this to bundle all our files to make them executable.
Type “jar cvf yourfilename.jar yourfilename.class”.
Editing MANIFEST.MF from JAR
Step 6: Thirdly, try running your jar file using “java -jar yourfilename.jar”. You will encounter an error “no main manifest attribute, in yourfilename.jar”. This is caused because the jar file we created creates a default manifest.mf file which does not know where your main method is located.
(Check this by opening jar file as ZIP. It is located inside “META-INF/MANIFEST.MF”)
Step 7: Now we need to add the main method located class to the manifest file from outside of the jar file.
Step 8: Let’s create a simple text file named “manifest.txt”. Add “Main-Class: yourfilename”, “yourfilename.java” must contain the main method which executes your whole program. Make sure you enter an extra line of empty space at the end. Save the file.
***Do not try to add this manually to the MANIFEST file inside JAR file, It will corrupt it.***
Step 9: Go back to command prompt and type “jar cmf manifest.txt yourfilename.jar yourfilename.class”. This command will add the main method located class inside “MANIFEST.MF” file.
Step 10: Let’s check if the JAR file runs now. It works!
Convert JAR file to EXE file using Launch4j 3.5
Step 11: Finally, we can convert to executable by converting our JAR file to EXE file. For this, we require you to download the application Launch4j here.
***Reminder: Please install the 3.5 version, another version may cause error because it might require you to sign the EXE file***
Launch the app.
JAR to EXE: How to Use Launch4j?
- Choose where you would like to place your EXE file by naming it in “Output file:”
- Select the Jar file we created from “Jar”.
- Use “Wrapper manifest:” for UAC(User Account Control)
- “Icon:” is used to select your desired icon for the EXE file
- Hold your mouse for further queries of other functons in textfields
Step 13: We need to define JRE minimum version for EXE to be compatible with. Check your JRE(Java Runtime Environment) by typing “java -version” in command prompt. Select your java version or earlier.
Step 14: Click the floppy disk icon in the top left to save the .xml file anywhere you like.
Step 15: Then click the settings icon in top left to create the EXE file, which should give you a similar message like this:
Step 16: Locate the EXE file where you saved it, and run it! JAVA to EXE is done!
In conclusion, the complete guide to convert a JAVA file to an executable file is fairly easy but requires no mistakes. Now, you do not need the JAVA environment to run your file. If you encounter any errors and/or have suggestions, please leave a comment below! We will get back to you as soon as possible!
Read our other articles:
Co-owner of Xiloop Technologies and Technoloxy.
Tech enthusiast, constantly learning and sharing knowledge.