
java Command in Linux
Java is a high-level programming language commonly used for web application development. It is available and compatible with all major operating systems like Linux, macOS, and Windows. In Linux operating systems, the "java" command is used to run or start a Java application. However, these applications must be compiled, which can be done using the javac command.
Table of Contents
Here is a comprehensive guide to the options available with the java command −
- How Does java Command Work?
- Syntax of java Command
- Options java Command
- How to Check if java Command is Installed?
- How to Use java Command in Linux?
How Does java Command Work?
The java command in Linux allows us to run a compiled Java application. When we execute this command, it initiates the Java Virtual Machine (JVM) in the background, loads the specified class, and invokes the main() method of that class. However, its important to note that the main() method must be declared public and static, must accept a String array as a parameter, and doesnt return any value.
Syntax of java Command
You can use the following syntax to run a Java application from a Linux terminal −
java [ options ] className [ argument ... ] java [ options ] -jar fileName.jar [ argument ... ]
The first syntax runs a compiled Java class file while the other one executes a Java application packaged in a JAR (Java Archive) file. Here, the options represent command-line options, className is the class to be invoked, and fileName indicates the JAR file that needs to be run. Moreover, the argument represents the arguments to be passed to the main() method.
Options java Command
Linux java command supports several options that are listed in the following table along with the description −
Option | Description |
---|---|
-agentlib:libname[=options] | It loads the specified native agent library. You can add options after a comma. |
-agentpath:pathname[=options] | It loads the native agent library specified by an absolute path. |
--class-path classpath, -classpath classpath, or -cp classpath | This option specifies the directories and JAR files to search for class files. |
--disable-@files | It halts further expansion of @-argfiles after this option. |
--enable-preview | It allows classes to depend on preview features. |
--module-path modulepath... or -p modulepath | It specifies directories that contain modules. |
--upgrade-module-path modulepath... | It specifies directories that replace upgradeable modules in the runtime image. |
--add-modules module[,module...] | It specifies root modules to resolve in addition to the initial module. |
--list-modules | It lists observable modules and then exits. |
-d module_name or --describe-module module_name | It describes a specified module and then exits. |
--dry-run | It creates the VM without executing the main method. |
--validate-modules | It validates all modules and exits. Useful for finding conflicts and errors. |
-Dproperty=value | It sets a system property value. Use quotes for values with spaces. |
-disablesystemassertions or -dsa | It disables assertions in all system classes. |
-enablesystemassertions or -esa | It enables assertions in all system classes. |
-help, -h, or -? | It prints the help message to the error stream. |
--help | It prints the help message to the output stream. |
-javaagent:jarpath[=options] | It loads the specified Java programming language agent. |
--show-version | It shows the product version to the output stream and continues. |
--show-module-resolution | It shows module resolution output during startup. |
-splash:imagepath | It shows a splash screen with the specified image. |
-verbose:class | It displays information about each loaded class. |
-verbose:gc | It shows information about each garbage collection event. |
-verbose:jni | It shows information about the use of native methods and JNI activity. |
-verbose:module | It shows the information about the modules in use. |
--version | It prints the product version to the output stream and exits. |
-version | It returns the product version to the error stream and exits. |
--help-extra | It shows help with extra options to the output stream. |
-X | It prints help on extra options to the error stream. |
@argfile | It specifies one or more argument files, expanding their contents for command-line options. |
Apart from these options, the java command supports some other options as well. You can access the manual page of the java command to learn more about it and its option −
man java

How to Check if java Command is Installed?
You can verify the existence of the Linux java command by checking its version. If the command shows the Java version. This means Java is installed on your Linux system −
java -version

If Java is not installed on your system, you will encounter a command not found error. In such a case, you can the following command to install Java on your Linux system −
#for Ubuntu or Debian sudo apt install default-jdk #for RHEL/CentOS/Fedora sudo yum install java-11-openjdk #For Arch Linux sudo pacman -S jdk-openjdk
How to Use java Command in Linux?
Lets go through the following steps to learn the use of a java command in Linux −
Step 1: Create a Java File
Lets create a simple Java file in any editor like Nano −
nano javaExample.java
Now paste the following code into this file to print a greeting message on the console −
public class ExampleClass{ public static void main(String[] args) { System.out.println("Hi Geeks, Welcome to Tutorialspoint!"); } }
Save the file and exit it.
Step 2: Compile the Java File
Now use the javac command to compile the javaExample.java −
javac javaExample.java
This command will create a compiled file with the ".class" extension, as shown below −

Step 3: Run the Java File
Now you can run the compiled Java using the java command, as shown below −
java javaExample.class
On successful execution of this command, you will get the desired output, as follows −

Similarly, you can use the java command to run a JAR file. However, in that case, you must specify the -jar option before the class name, as shown below −
java -jar fileName.jar
Thats all about the use of java command in Linux.
Conclusion
Java is a powerful, high-level programming language that is widely used for web application development across various operating systems, including Linux. The java command is a useful utility for developers that allows them to run compiled Java applications efficiently. The java command initiates the Java Virtual Machine (JVM), loads the specified class, and executes the main() method.
In this tutorial, we covered the basic syntax of the java command, its various options, how to check for Java installation, and provided a step-by-step guide on creating, compiling, and running a Java program in a Linux environment.