Creating Executable Jars
A .jar
file can be used to distribute a Java program. A .jar
file is essentially a .zip
file that follows a specific structure. This page describes how to use IntelliJ to create a .jar
file that can be executed from the command line.
Jar with JavaFX packages included (preferred)
If the computer intended to run the program does not have JavaFX installed on it, it is possible to bundle the JavaFX libraries in the .jar
but it requires more steps and results in a larger file.
Configuring Jar to be Created
- Create the following
Launcher
class
public class Launcher {
public static void main(String[] args) {
// Replace "Main" with the name of the class that extends Application
// See https://stackoverflow.com/a/52654791/3956070 for explanation
Main.main(args);
}
}
- Go to File -> Project Structure... (CTRL+ALT+SHIFT+S)
- Select Artifacts
- Click +
- Select JAR -> From modules with dependencies...
- Select the class
Launcher
class - Click OK
- Click the + below the Output Layout tab and select Directory Content
- Select the
C:\Program Files\Java\javafx-sdk-19\bin
folder - Click OK twice
Creating the Jar
- Go to Build -> Build Artifacts
- Select the Build action in the pop-up menu
- The
.jar
file should be located in a folder in theout\artifacts
folder of your project
Running the Jar file
You should be able to run the .jar
file on any Windows system with Java, but not necessarily JavaFX, installed. Move the .jar
file to your project folder (same folder as the src
and .iml
file). Then use the following command (from the command line):
java -jar WHATEVER.jar
Jar without JavaFX packages
This approach requires that the computer on which the .jar
is to be executed already has JavaFX installed and configured.
Configuring Jar to be Created
- Go to File -> Project Structure... (CTRL+ALT+SHIFT+S)
- Select Artifacts
- Click +
- Select JAR -> From modules with dependencies...
- Select the class that contains the
main
method (class that extendsApplication
) - Click OK
- In the Output Layout tab, remove all but the last item:
- Select Extracted ‘javafx-swf.jar/‘ and click -
- Remove the remaining entries that begin with Extracted leaving just one entry containing compiled output
Creating the Jar
- Go to Build -> Build Artifacts
- Select the Build action in the pop-up menu
- The
.jar
file should be located in a folder in theout\artifacts
folder of your project
Running the Jar file
You should be able to run the .jar
file on any Windows system with Java and JavaFX installed. Move the .jar
file to your project folder (same folder as the src
and .iml
file). Then use the following command (from the command line):
java —module-path C:\Java\javafx-sdk-19\lib —add-modules javafx.controls,javafx.fxml,javafx.swing -jar WHATEVER.jar
Of course, you’ll need to modify the module path to where you installed JavaFX.