java
Run Java programs
TLDR
Execute a Java .class file that contains a main method by using just the class name
Execute a Java program and use additional third-party or user-defined classes
Execute a .jar program
Execute a .jar program with debug waiting to connect on port 5005
Display JDK, JRE and HotSpot versions
Display help
SYNOPSIS
java [options] mainclass [args...]
java [options] -jar jarfile [args...]
PARAMETERS
-classpath path
-cp path
Specifies the search path for application classes and resources. Directories are separated by `:` on Linux/macOS or `;` on Windows.
-jar jarfile
Executes a program encapsulated in a JAR (Java Archive) file. The main class is specified by the `Main-Class` manifest header in the JAR.
-D
Sets a system property value. These properties can be accessed by the Java application at runtime using `System.getProperty()`.
-Xmx
Sets the maximum Java heap size. For example, `-Xmx512m` for 512 megabytes or `-Xmx2g` for 2 gigabytes.
-Xms
Sets the initial Java heap size. It's often recommended to set this to the same value as `-Xmx` to prevent resizing during execution.
-version
Prints the product version and exits. Displays information about the Java runtime environment.
-help
--help
Prints a help message for standard options and exits.
-agentlib:
Loads a native agent library. Used for profiling, debugging, and monitoring JVM behavior.
-javaagent:
Loads a Java programming language agent. Used for bytecode instrumentation, often by frameworks like AspectJ or monitoring tools.
DESCRIPTION
The `java` command is the primary launcher for Java applications, serving as the interface to the Java Virtual Machine (JVM).
It interprets and executes Java bytecode, enabling platform-independent execution of compiled Java programs.
This command is fundamental for running any Java-based software, from simple command-line utilities to complex server-side applications. When invoked, `java` loads the specified class file or JAR archive, initializes the JVM, and begins executing the application's `main` method.
It handles various configurations through its extensive set of options, allowing users to control aspects like memory allocation, classpath, system properties, and debugging modes, making it a versatile tool for both development and deployment environments.
CAVEATS
While Java aims for 'write once, run anywhere', platform-specific differences can sometimes occur, particularly with native libraries or UI toolkits.
Memory management with the JVM can be complex; misconfigured heap sizes (e.g., `-Xmx`) can lead to 'Out Of Memory' errors or inefficient garbage collection.
Security is a crucial aspect; running untrusted Java applications or applets can pose risks, although modern Java security models are robust.
Version compatibility is another consideration; applications compiled with newer Java versions may not run on older JVMs, and vice-versa.
<B>EXECUTION MODES</B>
The `java` command supports two primary execution modes:
1. Directly executing a class file: `java com.example.MyMainClass` requires the JVM to locate `MyMainClass.class` on the specified classpath.
2. Executing a JAR file: `java -jar myapp.jar` relies on the JAR's manifest file to specify the main class, simplifying deployment and packaging.
<B>JVM ARGUMENTS VS. APPLICATION ARGUMENTS</B>
Arguments passed directly after `java` (e.g., `-Xmx`, `-D`) are JVM arguments, controlling the virtual machine itself.
Arguments passed after the `mainclass` or `jarfile` (e.g., `arg1 arg2`) are application arguments, which are received by the Java application's `main` method. This distinction is crucial for proper command invocation.
HISTORY
The Java programming language and its runtime environment were initially developed by James Gosling at Sun Microsystems and first released in 1995.
The `java` command has been central to the Java ecosystem since its inception, evolving alongside the language and JVM. After Oracle acquired Sun Microsystems in 2010, the development continued under Oracle's stewardship, with significant contributions from the open-source OpenJDK community.
Its design principle of 'write once, run anywhere' rapidly propelled Java to prominence for enterprise applications, web services, and Android mobile development, making the `java` command an indispensable tool for developers and system administrators globally.
SEE ALSO
javac(1) - Java compiler, compiles Java source files to bytecode., jar(1) - JAR archive tool, creates and manages JAR files., javap(1) - Java class file disassembler, prints information about class files., jps(1) - JVM Process Status Tool, lists instrumented HotSpot Java Virtual Machines on the target system.