- Create Windows Executable (.exe) for Java Application
- How to make an Executable File from your Java Code
- What is JAR File?
- What is an Executable file (.exe)?
- How to convert your java project into a .exe file?
- Number of Steps to be needed to convert java projects into .exe file
- Convert Java Programs/Projects into one .exe file
- Step 1: Choose your Java Project from the Eclipse
- Make a Java File Executable
- Make a Java File Executable using JAR Command
- Make a Java File Executable using IDE
- Make a Java File Executable using External Tools and Libraries
Create Windows Executable (.exe) for Java Application
Launch4j is a cross-platform tool for wrapping Java applications distributed as jars in lightweight Windows native executable files. In this post, we will learn making such an executable file for a demo java application.
Step1 ) Create a java application
I am creating a very basic java class which simply displays a frame and some text on it. It also has the main() method which will start the application.
package com.howtodoinjava; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Label; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JDialog; public class ApplicationMain extends JDialog < private static final long serialVersionUID = 1L; public ApplicationMain() < //Create a frame Frame f = new Frame(); f.setSize(500, 300); //Prepare font Font font = new Font( "SansSerif", Font.PLAIN, 22 ); //Write something Label label = new Label("Launch4j Maven Demo with HowToDoInJava.com"); label.setForeground(Color.RED); label.setFont(font); f.add(label); //Make visible f.setVisible(true); f.addWindowListener(new WindowAdapter() < public void windowClosing(WindowEvent e) < System.exit(0); >>); > public static void main(final String[] args) < new ApplicationMain(); >>
Step 2) Create maven file with launch4j configured in it
I have created a pom file for packaging the application as .exe file. If you feel something unclear, drop a comment.
4.0.0 com.howtodoinjava JavaExeDemo jar 1.0-SNAPSHOT JavaExeDemo http://maven.apache.org UTF-8 org.apache.maven.plugins maven-compiler-plugin 2.5.1 1.6 org.apache.maven.plugins maven-shade-plugin 1.7.1 package shade true shaded com.howtodoinjava.Main com.akathist.maven.plugins.launch4j launch4j-maven-plugin 1.5.1 l4j-clui package launch4j gui $/$ -$ -shaded.jar $/howtodoinjava.exe http://java.com/download com.howtodoinjava.ApplicationMain anything application.ico 1.6.0 preferJre 1.0.0.0 $ $2012 howtodoinjava.com 1.0.0.0 1.0.0.0 $howtodoinjava.com howtodoinjava howtodoinjava.exe
Step 3) Create .exe file
To make the exe file for above java program, run maven command:
Above command will create the “howtodoinjava.exe ” file in your project’s target folder. Double click on .exe file will open the window like this.
If you want to download source code of above application, click on below given download link.
Happy Learning !!
How to make an Executable File from your Java Code
Hello dear visitors, your most welcome in this domain(www.bittutech.com). Today, we are going to know about How to change/convert a Java Project/Program into an Executable file format? We have seen that mostly Java Project/Program contains two type of major files – (1) .java file and (2) .class file.
We would have studied Java in the first semester of Graduation in the Computer Science/Computer Application field. We would have made many programs and projects during that time, but probably, we wouldn’t have learned that How to convert a java project into a java archive file and convert the java archive file to an Executable file format.
Don’t bother from it, we will learn those things in this post and we will know that How to execute Java Program without JRE(Java Runtime environment).
First of all, we are going to know about JAR and EXE file format with brief introduction.
What is JAR File?
JAR file is also known as Java Archive file, which is used to encapsulate your java projects including package, classes, and objects with images and documentation in one file. JAR files work on distribute mechanisms to give our programs or projects to others.
It is a combination of heterogeneous files such as java source code, java class file, text file for documentation. we can contain various packages and classes in one JAR(java archive) file. it also helps to distribute our program to others in an easy way.
What is an Executable file (.exe)?
Executable files are those files which contain only machine language or combination of 0’s and 1’s binary digits. we can execute these files without any external environment or without the help of virtual CPUs. (.exe) the file is a type of Executable file that basically runs on Windows OS with the help of physical built-in CPUs.
Each and every program basically passes through three categories.
How to convert your java project into a .exe file?
Most often, we are using an Eclipse-based built-in java interpreter to run all Java projects either graphical projects or command-based projects. But if we want to do something different, and don’t want to use JRE(Java run-time environment) to execute our program without the help of JVM(java virtual machine), so we have to perform some basic work and after we can execute our java projects without the help of any downloaded virtual CPU. It means we have to convert our java projects into .exe file format.
Number of Steps to be needed to convert java projects into .exe file
There will follow multiple steps to convert our Java Projects into Executable file format(.exe). Before moving on towards, we have to know that What are the advantages of converting your java projects in .exe file?
- A .exe file doesn’t depend on virtual processors.
- A .exe file is a type of executable file for simply run on a Windows machine.
- A .exe file contains machine code and runs faster than others.
- A .exe file can run on another Windows PC without any external environment.
- A .exe file is also known as a binary file or non-human readable file.
Convert Java Programs/Projects into one .exe file
Finally, we are going to see that How to convert java projects into .exe file.
Step 1: Choose your Java Project from the Eclipse
You have to choose your project from Eclipse Java development Application and you have to follow several simple steps to firstly convert your java project into JAR file then you may be able to convert your JAR file into .exe file. Follow the below Screenshots.
First Step : Open Eclipse and Choose your Java project then Right Click on your project and select the Export Option.
Step 2: After select Export Option, select the Runnable Jar file option from the Next Window.
Step 3: Choose the Export Destination directory and file name.
Step 4: Click the Finish Button and check your Jar file is existing or not in your intended directory.
Step 5: Download launch4J application on your windows PC/Laptop to convert your JAR file into .exe format.
Step 6: After downloading the Application, double click on the downloaded exe file and install it on your PC/Laptop.
Step 7: Launch the Launch4J application after installing on your machine.
Step 8: Choose the Output file Location and give the Jar file location. you can also add file icon, but it is not necessary.
Step 9: After Basic Setting, you have to jump on the Jar tab for setting the minimum JRE version requirements.
Last Step: after completing the setting, you have to click on the Gear icon or Setting icon at the top of the below window.
So, now you can see your .exe file with/without icon. I use Google icon in place of by default exe icon. GUI_Project.exe is my Java project and i can run on any windows machine without the help of JVM or JRE.
Last words: That’s it for now. We will meet again with a new interesting article. Stay tuned with us. You can join us on social networks and get the latest update for newer post.
Make a Java File Executable
- Make a Java File Executable using JAR Command
- Make a Java File Executable using IDE
- Make a Java File Executable using External Tools and Libraries
This tutorial introduces how to convert a Java program to an executable jar file in Java and also lists some example codes to understand the topic.
- jar command
- IDE (Eclipse, IntelliJ IDEA)
- Some tools like javapackager, WinRun4J, packr, JSmooth, JexePack, InstallAnywhere, Launch4j, etc.
Make a Java File Executable using JAR Command
We will start with a simpler method that requires just a single command that we need to run via a terminal. We will use compiled Java file, so make sure to compile the file first. Then, we will open the terminal, write the following command, and hit enter.
jar -cvf jarfile.jar MainJavaFile.class
-cvf is a flag that represents copy , verbose , file , respectively.
jarfile.jar is the name of the JAR file that we want to create.
MainJavaFile is the main Java file that will be used as a source file.
After executing this command, it creates a JAR file that contains a menifest.mf file. It is a special file that contains information about the files packaged in a JAR file.
We need to open this and provide a main-class execution path, like:
Main-class: packageName.MainJavaFile
Write this line to the file, save and quit, and execute this command to run the JAR file in the terminal.
It will run the Java code and display the output to the console or open a new window if the JAR file belongs to a graphical application such as swing or JavaFX.
Make a Java File Executable using IDE
We can use any IDE to create JAR file from a Java file. Here, we will use Eclipse IDE to create JAR. It includes the following steps.
Go to File menu and select Export
Select Runnable JAR file
Select Java file from Launch Configuration dropdown
Select Location to store the JAR, and
Execute the JAR
To create JAR using Elipse IDE, follow the above steps, and you will get the JAR.
Make a Java File Executable using External Tools and Libraries
WinRun4j is a java launcher for windows. It is an alternative to javaw.exe and provides several benefits. It uses an INI file for specifying classpath, main class, vm args, etc. It uses additional JVM args for more flexible memory use. We can use it to make a JAR runnable for Windows compatible.
It is a cross-platform tool for wrapping Java applications as JARs in lightweight Windows executables. It is possible to set runtime options, like the initial/max heap size.
Copyright © 2023. All right reserved