Running Java from the command line remains the most direct way to interact with the Java Virtual Machine. This method strips away the abstractions of IDEs and build tools, revealing the core mechanics of how your code executes. Understanding this process demystifies the build lifecycle and provides essential troubleshooting skills for any developer working with Java.
Setting Up the Environment
Before you can execute any Java application, you must ensure your system is configured correctly. The primary requirement is the Java Development Kit (JDK), which includes the Java Runtime Environment (JRE) and development tools like the compiler. You need to set the JAVA_HOME environment variable to point to your JDK installation directory and update the system PATH to include the bin directory. Without this configuration, the operating system will not recognize commands like java or javac .
Compiling Your Java Source Code
The first step in the command-line workflow is transforming human-readable Java code into bytecode. You use the javac compiler, followed by the name of your source file, which must end with the .java extension. For example, to compile HelloWorld.java , you would type javac HelloWorld.java . This command generates a .class file, which contains the bytecode that the JVM can understand. If your code relies on external libraries, you must include them in the classpath during this compilation stage using the -cp or -classpath flag.
Handling Package Declarations
Java files that declare a package require a specific directory structure that mirrors the package name. If your code begins with package com.example.utils; , you must run the javac command from the parent directory of the com folder. The compiler will then recreate the directory structure and place the resulting .class file in the correct location. When running the program, you must also be in the parent directory of the package root to ensure the JVM can locate the class using its fully qualified name.
Executing the Bytecode
Once you have your compiled .class file, you can launch the application using the java command. It is crucial to understand that you do not include the .class extension when executing the command. Instead, you specify the fully qualified name of the class containing the public static void main(String[] args) method. For a class named Main in the default package, the command is simply java Main . If the class resides in a package, you must use the full name, such as java com.example.app.Main .
Leveraging the Classpath
The classpath is a critical parameter that tells the JVM where to look for user-defined classes and packages. By default, the JVM searches the current directory. However, when your dependencies are in separate JAR files or folders, you must define the classpath manually using the -cp or -classpath option. You can include multiple locations by separating them with a colon on Unix-based systems or a semicolon on Windows. For instance, java -cp .:lib/* com.example.App tells the JVM to look in the current directory and the lib folder for required classes and libraries.