Java applications are often packaged as JAR (Java Archive) files. These files bundle all the necessary classes, resources, and metadata into a single, distributable unit. This tutorial explains how to execute these JAR files directly from the command line, providing a fundamental skill for Java developers and system administrators.
Understanding JAR Files
A JAR file is essentially a ZIP archive, but with a specific structure understood by the Java Virtual Machine (JVM). It can contain Java class files (.class), images, audio, and other resources. Crucially, a JAR file can also specify an entry point – a main class – that the JVM should execute when the JAR is run.
Running a JAR File with a Defined Entry Point
The most common way to run a JAR file is using the java -jar
command. This command tells the JVM to execute the main class specified within the JAR’s manifest file.
The syntax is simple:
java -jar <jar-file-name>.jar
Replace <jar-file-name>
with the actual name of your JAR file (e.g., myApplication.jar
). For example:
java -jar myApplication.jar
The JVM will locate the Main-Class
attribute in the JAR’s MANIFEST.MF
file (located within the META-INF directory of the JAR) and execute the main
method of the specified class.
Running a JAR File Without a Defined Entry Point, or Specifying a Different Main Class
Sometimes, you might want to run a JAR file that doesn’t have a Main-Class
specified in its manifest, or you want to run a different class’s main
method than the one defined in the manifest. In these cases, you can use the -cp
(or -classpath
) option.
The -cp
option tells the JVM where to find the class files. You need to specify the JAR file itself, and then the fully qualified name of the class containing the main
method you want to execute. The syntax is:
java -cp <jar-file-name>.jar <fully.qualified.ClassName>
For example, if your JAR file is myLibrary.jar
and the class containing the main method is com.example.MyApplication
, you would use:
java -cp myLibrary.jar com.example.MyApplication
This instructs the JVM to load the classes from myLibrary.jar
and execute the main
method of the com.example.MyApplication
class.
Important Considerations:
- File Paths: Ensure you provide the correct path to the JAR file. If the JAR file is not in the current directory, you must specify the full or relative path.
- Dependencies: If your application depends on other JAR files, you need to include them in the classpath as well. You can do this by separating multiple JAR files or directories with colons (:) on Linux/macOS or semicolons (;) on Windows. For example:
java -cp myapp.jar:lib/dependency1.jar:lib/dependency2.jar com.example.MyApplication
. - JVM Options: You can also pass other JVM options before the
-jar
or-cp
arguments. For example, to set the maximum heap size, you could use:java -Xmx512m -jar myapp.jar
.
By understanding these methods, you can effectively execute JAR files from the command line, providing a versatile way to run and test your Java applications.