How do I run a Java program from the command line on Windows?
Categories:
How to Run a Java Program from the Windows Command Line

Learn the essential steps to compile and execute Java applications directly from the command prompt on Windows, covering environment setup and common commands.
Running Java programs from the command line is a fundamental skill for any Java developer. While Integrated Development Environments (IDEs) like IntelliJ IDEA or Eclipse automate much of this process, understanding the underlying commands provides deeper insight into how Java applications are compiled and executed. This guide will walk you through setting up your environment, compiling your Java source code, and running the resulting bytecode on a Windows system.
1. Verify Java Development Kit (JDK) Installation
Before you can compile or run Java programs, you need to have the Java Development Kit (JDK) installed on your system. The JDK includes the Java Runtime Environment (JRE), the Java compiler (javac
), and other essential development tools. If you only have the JRE, you'll be able to run programs but not compile them.
1. Open Command Prompt
Press Win + R
, type cmd
, and press Enter
.
2. Check Java Version
In the command prompt, type java -version
and press Enter
. This command checks if the JRE is installed and configured correctly.
3. Check Java Compiler Version
Next, type javac -version
and press Enter
. This command verifies if the JDK (specifically the compiler) is installed and accessible via your system's PATH environment variable. If you receive an error like 'javac' is not recognized as an internal or external command
, it means the JDK is either not installed or its bin
directory is not in your system's PATH.
javac
is not recognized, you'll need to install the JDK and/or configure your system's PATH environment variable. Typically, the JDK's bin
directory (e.g., C:\Program Files\Java\jdk-17\bin
) needs to be added to PATH.2. Write Your Java Program
For this guide, we'll use a simple "Hello, World!" program. You can use any text editor (like Notepad, VS Code, or Sublime Text) to create your Java source file. Remember that the filename must exactly match the public class name, including case, and end with the .java
extension.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Command Line Java!");
}
}
3. Compile Your Java Program
Once your Java source file is saved, you need to compile it into bytecode. The Java compiler (javac
) translates your human-readable .java
file into machine-readable .class
files, which contain Java bytecode. These .class
files are what the Java Virtual Machine (JVM) executes.
1. Navigate to Your Program's Directory
Open the Command Prompt and use the cd
command to navigate to the directory where you saved your HelloWorld.java
file. For example, if you saved it in C:\JavaPrograms
, you would type cd C:\JavaPrograms
.
2. Compile the Program
Execute the Java compiler by typing javac HelloWorld.java
and pressing Enter
. If there are no compilation errors, the command prompt will return without any output. A new file named HelloWorld.class
will be created in the same directory.
javac
will display them in the command prompt, indicating the line numbers and types of errors. You'll need to fix these in your .java
file and recompile.4. Run Your Java Program
After successful compilation, you can run your Java program using the java
command. This command invokes the Java Virtual Machine (JVM), which loads the .class
file and executes its bytecode.
1. Execute the Program
In the same command prompt window (still in the directory containing HelloWorld.class
), type java HelloWorld
and press Enter
. Notice that you do not include the .class
extension when running the program.
2. Observe Output
The program will execute, and you should see the output: Hello, Command Line Java!
printed to the console.
flowchart TD A[Write Java Source Code (HelloWorld.java)] --> B{Is JDK Installed and PATH Configured?} B -- No --> C[Install JDK & Configure PATH] B -- Yes --> D[Open Command Prompt] D --> E[Navigate to Source Directory (cd path/to/java/files)] E --> F[Compile Source Code (javac HelloWorld.java)] F -- Compilation Errors --> G[Fix Errors in HelloWorld.java] F -- Success --> H[HelloWorld.class Created] H --> I[Run Java Program (java HelloWorld)] I --> J[Program Output Displayed]
Process Flow for Compiling and Running a Java Program
5. Running Programs with Packages
For more complex applications, Java programs are often organized into packages. When dealing with packages, the compilation and execution commands require slight adjustments to reflect the package structure.
package com.example.app;
public class MyProgram {
public static void main(String[] args) {
System.out.println("Running program with packages!");
}
}
1. Create Directory Structure
Create the directory structure that matches your package declaration. For package com.example.app;
, you'd create com\example\app
and place MyProgram.java
inside the app
folder.
2. Compile from Root Directory
Navigate to the root directory containing the com
folder (e.g., C:\JavaPrograms
). Then, compile using javac com\example\app\MyProgram.java
.
3. Run from Root Directory
From the same root directory, run the program using the fully qualified class name: java com.example.app.MyProgram
.
java
command from the directory that is the parent of your top-level package (e.g., the directory containing the com
folder), and you must use the fully qualified class name (e.g., com.example.app.MyProgram
).