Using the java command (with examples)
1: Executing a java .class file
java classname
Motivation: This use case is used to execute a Java program that contains a main method. It is the most common and straightforward usage of the java
command.
Explanation: The java
command is followed by the name of the class file to be executed. The class file must contain a main
method, which is the entry point of the Java program.
Example Output:
Hello, World!
If the classname
class file contains a main
method that prints “Hello, World!”, executing java classname
will display “Hello, World!” on the console.
2: Executing a java program with additional classes
java -classpath path/to/classes1:path/to/classes2:. classname
Motivation: This use case is used when a Java program depends on external or user-defined classes that are not in the same directory as the class file being executed.
Explanation: The -classpath
(or -cp
) option allows specifying additional directories or JAR files where the Java runtime should look for classes. The value of the -classpath
option is a colon-separated list of directories/JAR files. The .
(dot) represents the current directory.
Example Output:
Hello, John!
Assume that the class Greeting
is in the com/example/util
package, located in the directory path/to/classes1
. When executing java -classpath path/to/classes1:. com.example.Main
, the Java program will use the Greeting
class from path/to/classes1
and display “Hello, John!” on the console.
3: Executing a .jar program
java -jar filename.jar
Motivation: This use case is used to execute Java programs that are bundled as executable JAR files. JAR files provide a way to package all the necessary classes and resources into a single file.
Explanation: The -jar
option specifies that the Java program should be executed from a JAR file. The filename.jar
is the name of the JAR file to be executed.
Example Output:
Hello, World!
If filename.jar
is a JAR file that contains a Main
class with a main
method that prints “Hello, World!”, executing java -jar filename.jar
will display “Hello, World!” on the console.
4: Executing a .jar program with debug
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 -jar filename.jar
Motivation: This use case is used when we need to enable remote debugging for a Java program in a JAR file. It allows us to connect a debugger to the running Java process.
Explanation: The -agentlib:jdwp
option enables Java Debug Wire Protocol (JDWP) for debugging. The transport=dt_socket
indicates that the debugger will connect through a socket. The server=y
tells the JVM to listen for a debugger connection. The suspend=y
pauses the JVM until a debugger connects. The address=*:5005
specifies the hostname and port (5005 in this case) on which the JVM should listen for debugger connections.
Example Output:
Listening for transport dt_socket at address: 5005
Executing java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 -jar filename.jar
will pause the JVM and provide the output indicating that it is waiting for a debugger to connect on port 5005.
5: Displaying JDK, JRE, and HotSpot versions
java -version
Motivation: This use case is used to quickly check the version of the installed JDK (Java Development Kit), JRE (Java Runtime Environment), and HotSpot (Java Virtual Machine).
Explanation: The -version
option is a built-in option of the java
command that displays the version information of the installed Java runtime environment.
Example Output:
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
Executing java -version
will display the version information of the currently installed Java runtime environment.
6: Displaying usage information for the java command
java -help
Motivation: This use case is used to get detailed information about the usage of the java
command, including all available options and arguments.
Explanation: The -help
option is a built-in option of the java
command that displays the usage information.
Example Output:
Usage: java [options] <mainclass> [args...]
Commands:
...
-h, --help print this help message
...
Executing java -help
will display detailed usage information for the java
command, including available options and arguments.