Java Command Line Execution | Online Free DevTools by Hexmos

Learn how to execute Java programs from the command line. This guide covers running JAR files, passing arguments, setting memory, and using the classpath with clear examples.

Java Command Line Execution Guide

This section provides essential command-line instructions for executing Java programs and managing their execution environment. Understanding these commands is crucial for efficient Java development and deployment.

Executing JAR Files

The primary method to run a Java application packaged as a JAR (Java Archive) file is using the java -jar command. This command tells the Java Virtual Machine (JVM) to execute the main class specified in the JAR's manifest file.

# To execute a java program
# To run a java file
java -jar <filename.jar>

Passing Arguments to Java Programs

Many Java applications require input arguments to function correctly. These arguments can be passed directly after the JAR file name on the command line. The program can then access these arguments via the String[] args parameter in its main method.

# To pass arguments to your java program
java -jar <filename.jar> <arg1> <arg2> ...
java -jar example.jar "Hello world" 1234

Checking Java Version

It's often necessary to verify the installed Java Development Kit (JDK) or Java Runtime Environment (JRE) version. The java -showversion command displays detailed information about the Java environment, including the version, build details, and VM type.

# To get the version of the installed java environment
java -showversion

Example output:

openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Configuring JVM Memory Settings

Java applications can be memory-intensive. You can control the memory allocated to the JVM using flags like -Xms (initial heap size), -Xmx (maximum heap size), and -Xss (thread stack size). Proper memory configuration can significantly impact performance and prevent out-of-memory errors.

# To set the initial memory size to be used by the program
# Use -Xms<size> to set initial memory allocation
# Use -Xmx<size> to set maximum allowed memory allocation
# Use -Xss<size> to set maximum allowed thread stack size
# Initially assigns 256mb and allows up to 2gb, thread stack size of 1mb
java -Xms256m -Xmx2g -Xss1m -jar <filename.jar>

Utilizing the Classpath

The classpath tells the JVM where to find user-defined classes and packages. You can specify the classpath using the -classpath (or its shorthand -cp) option. This setting overrides the system's CLASSPATH environment variable for the current session only.

# To use the classpath variable
# It overrides the CLASSPATH of Environment variable but only for that session.
# If you restart the application you need to again set the classpath variable.
# You can use either: -classpath, --classpath or -cp
java -classpath <classpath> -jar <filename.jar>
java -classpath "my/example/path/one:my/example/path/two" -jar myfile.zip

Executing Java Classes Directly

If you have a compiled Java class file (.class) and want to execute it directly without creating a JAR, you can use the java -cp command. Ensure the classpath points to the directory containing the class file.

# To execute a java class
# Say you have a java class of name HelloWorld in a file called HelloWorld.java
# Say you want to execute your class HelloWorld and pass it the argument "hello"
# Say the file is the local directory we use the options "-cp ."
java -cp <path> <class> <arg>
java -cp . HelloWorld "hello"

For more in-depth information on Java development and command-line tools, refer to the official Java documentation.