How to Make a Java File Executable

Mohammad Irfan Feb 02, 2024
  1. Make a Java File Executable using JAR Command
  2. Make a Java File Executable using IDE
  3. Make a Java File Executable using External Tools and Libraries
How to Make a Java File Executable

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.

In Java, to create an executable JAR file, we can use several ways such as:

  • 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

Here, jar is the command.

-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.

java - jar jarfile.jar

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

Apart from these solutions, you can use several other tools such as:

  • javapackager

It is a standard tool provided by Oracle and can be used to perform tasks related to packaging Java and JavaFX applications. We can use the -createjar command of this tool to create a JAR.

  • WinRun4j

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.

  • packr

This tool is available at GitHub and can be easily used to packages your JAR, assets, and a JVM for distribution on Windows, Linux, and macOS. packr is most suitable for GUI applications. We can use it to create JAR files.

  • Launch4j

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.