3 minute read

How do we run a Java application without an IDE?

After finishing developing and testing in an IDE, before distributing it for deployment, it is good to test the application outside of the IDE.

A Java application is built into a JAR file. The JAR file will run from the command line.

1. JAR (Java Archive) File

A JAR (Java Archive) file is a compressed package that is executable by Java Runtime Environment (JRE). Multiple Java class files can be bundled into a single archive file with file extension .jar.

2. Creating the JAR File

Once a Java project has been completed inside an IDE, start to build the project and create a JAR file.

(For NetBeans)

Specify the Main Class

Be sure to have the main class specified in the project. To do so, right click the project name under the Projects tab. Select Set Configuration and then Customize. The value in the field Main Class should be a class that has the main method.

Build the Project

Right click the project name under the Projects tab. Select Clean and Build. Or in the top menu bar, Run -> Clean and Build Project.

The following change will be made during the build:

  • Two new folders, build and dist, are added to the project folder (referred to as the PROJECT_HOME folder).

  • All the sources are compiled into .class files, which are placed into the PROJECT_HOME\build folder.

  • A JAR file is created inside the PROJECT_HOME\dist folder.

  • Beside the JDK, if any libraries have been specified for the project, a lib folder will be created in the folder dist. The libraries will be copied into dist\lib.

  • The manifest file in the JAR is updated to include entries that designate main class and any libraries that are on the project’s classpath.

3. Example

Follow the instructions above, build a JAR file (inside the NetBeans) for Echo application (which is discussed in the post Java Review: Application.

After the build completes, navigate to the PROJECT_HOME/dist folder. There are two files named Echo.jar and README.TXT.

README.TXT contains the information about how to launch the application.

To read the content, open README.TXT in a text editor. Part of the content is copied below that are instructions of running the application.

To run the project from the command line, go to the dist folder and type the following:

java -jar "Echo.jar" 

To distribute this project, zip up the dist folder (including the lib folder) and distribute the ZIP file.

4. Launching the JAR File

To run the application from the command line,

  • Inside the dist folder

Navigate to the dist folder and type the following command:

java -jar "Echo.jar" coffee data 12 anything better
  • Outside of the dist folder

To run the application from the command line outside of the dist folder, enter the full path (absolute path) to the jar file in the command:

java -jar "C:\PROJECT_HOME\dist\Echo.jar" coffee data 12 anything better

Windows uses back slashes. Other operating systems and Web use forward slashes.

5. Distributing the Project

As indicated in READEME.TXT, to distribute the project,

  • Zip up the dist folder (including the lib subfolder if there is one)

  • Send the ZIP file to the people who should be the recipients.