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]
java [options] -m module[/mainclass] [args]
PARAMETERS
-version
Print product version and exit
-v or --version
Print brief version (Java 9+)
-cp <path> or -classpath <path>
Set classpath for classes/jars
--jar <jar>
Execute named jar file
-jar <jar>
Legacy jar execution (use --jar)
-m or --module <module>[/<mainclass>]
Run module (Java 9+)
-p or --module-path <path>
Module search path
-D<name>=<value>
Set system property
-Xmx<size>
Max heap size (e.g., -Xmx1g)
-Xms<size>
Initial heap size
-XX:+UseG1GC
Enable G1 garbage collector (default Java 9+)
-verbose:class
Verbose class loading
-verbose:gc
Verbose garbage collection
--enable-preview
Enable preview features
-agentlib:<libname>
Load native agent library
DESCRIPTION
The java command starts a Java Virtual Machine (JVM) to execute Java programs, applets, or JAR files. Part of the JDK/JRE, it loads bytecode, performs verification, and runs the main class method. Supports class files, executable JARs, and modules (Java 9+).
Common uses include running console apps, servers (e.g., Tomcat), or GUI tools. Options tune performance: memory allocation (-Xmx/-Xms), garbage collection (-XX flags), debugging (-agentlib), and security. Classpath management via -cp allows specifying libraries.
The JVM handles platform independence via Just-In-Time (JIT) compilation, converting bytecode to native code. Multiple implementations exist: OpenJDK (default on many Linux distros), Oracle JDK. Environment variables like JAVA_HOME and CLASSPATH influence behavior.
For production, set heap sizes to avoid OutOfMemoryError. Modular apps use -p/-m for better encapsulation. Verbose modes (-verbose:class/gc) aid troubleshooting. Exits with codes: 0 success, non-zero errors.
CAVEATS
Unlimited options exist (-X, -XX); consult java --help or man page. Heap too small causes OutOfMemoryError. Untrusted code risks security; use sandboxing. Multiple JVMs may conflict; specify full path.
ENVIRONMENT VARIABLES
JAVA_HOME: JDK install dir.
CLASSPATH: Default class path.
_JAVA_OPTIONS: Options passed to JVM.
JVM SELECTION
Use update-alternatives --config java on Debian/Ubuntu to switch versions.
HISTORY
Developed by Sun Microsystems (1995) for JDK 1.0. Evolved with HotSpot JVM (1999), generics (1.5), modules (9, 2017). OpenJDK project (2006) now dominant; Oracle JDK commercial. Linux support via packages like openjdk-17-jdk.


