Running a Java program from the command line is a fundamental skill that every developer should master. It provides a direct connection to the Java Virtual Machine, stripping away the abstractions of modern IDEs to reveal how code is actually executed. This process compiles your source files into bytecode and then instructs the JRE to run that bytecode, offering a clear view of the build and execution lifecycle.
Setting Up Your Environment
Before you can execute any Java application, you must ensure your system is configured correctly. The Java Development Kit (JDK) is required because it contains the compiler (`javac`) needed to translate your code. Simply having the Java Runtime Environment (JRE) is not sufficient for the compilation step.
To verify your installation, open your terminal or command prompt and type `java -version` and `javac -version`. If the commands return version numbers, your Path environment variable is set correctly. If not, you will need to add the `bin` directory of your JDK installation to the system Path, which allows the command line to locate the Java executables from any directory.
Writing Your First Java Class
Create a simple Java file using a text editor. It is crucial to match the filename exactly with the public class name inside the code. For instance, if your public class is named `HelloWorld`, the file must be saved as `HelloWorld.java`.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } The Compilation Step Navigation to the directory containing your Java file using the `cd` command. Once located, you compile the source code using the `javac` command. Running `javac HelloWorld.java` reads the `.java` file and generates a corresponding `.class` file.
The Compilation Step
This `.class` file contains bytecode, which is a platform-independent instruction set. If your code contains errors, the compiler will display error messages that you must fix before proceeding. Successful compilation leaves the terminal silent, producing only the new class file.
Executing the Bytecode
With the `.class` file now present, you can instruct the JRE to run it using the `java` command. It is important to note that you do not include the `.class` extension when executing. You simply run `java HelloWorld`.
At this stage, the JVM loads the class, verifies the bytecode, and invokes the `main` method. The string "Hello, World!" is then printed to the console, confirming that the environment is fully operational and the program logic is sound.
Handling Packages and Directory Structures
As projects grow, organizing code into packages becomes necessary. If your Java file declares a package statement, such as `package com.example.app;`, the directory structure must mirror this namespace. The file must reside in a folder path `/com/example/app/`.
When compiling and running, you must be in the root directory of this structure. You compile with `javac com/example/app/HelloWorld.java` and execute from the parent folder using `java com.example.app.HelloWorld`. This ensures the JVM can locate the class using its fully qualified name.
Troubleshooting Common Errors
Encountering errors is a standard part of the learning process. A frequent mistake is the `Error: Could not find or load main class` message, which usually indicates a typo in the class name or an incorrect classpath.
If you see `javac' is not recognized as an internal or external command`, it means the JDK bin directory is not in your system's Path. Setting the Path correctly or using the full path to the `javac` executable will resolve this issue, allowing the command line to locate the necessary tools.