Java

How do I run a Java program from the command line on Windows

19 September 2026 · 9 min read

How do I run a Java program from the command line on Windows

Have you ever felt lost trying to execute your Java masterpiece outside of an Integrated Development Environment (IDE)? Many developers, especially beginners, find themselves scratching their heads when attempting to run a Java program from the command line on Windows. It’s a crucial skill to master, as it allows for greater control over your application’s environment and simplifies tasks like scripting and automation. This guide will walk you through the process step-by-step, demystifying the command line and empowering you to execute your Java code with confidence. We will cover everything from setting up your environment variables to compiling and running your first program, ensuring you have a solid understanding of the fundamentals. Whether you’re a student, a hobbyist, or a professional developer, this article will equip you with the knowledge to seamlessly execute Java programs from the command line on your Windows machine, unlocking a new level of efficiency and control.

Setting Up Your Java Environment

Before you can run a Java program from the command line on Windows, you need to ensure that Java is properly installed and configured on your system. This involves downloading the Java Development Kit (JDK), installing it, and setting the necessary environment variables. The JDK provides the tools needed to compile and execute Java code. Without a correctly configured environment, the command line won’t be able to locate the Java compiler and runtime environment, leading to frustrating errors. Make sure you download the correct version of the JDK compatible with your system architecture (32-bit or 64-bit). Remember to always download the JDK from a trusted source like the Oracle website or OpenJDK builds.

The most important environment variable to configure is the JAVA_HOME variable. This variable points to the installation directory of your JDK. You also need to add the %JAVA_HOME%\bin directory to your Path variable. This allows you to execute Java commands like javac (the Java compiler) and java (the Java runtime environment) from any directory in the command line. Restarting your command prompt or even your computer after setting these variables is crucial to ensure the changes are applied correctly. Failing to do so can result in “command not found” errors when trying to use Java commands.

Here are the steps to set up your Java environment:

  1. Download the latest JDK from the Oracle website.
  2. Install the JDK, noting the installation directory.
  3. Open the System Properties dialog box (search for “environment variables” in the Windows search bar).
  4. Click “Environment Variables”.
  5. Under “System variables,” click “New…”.
  6. Enter JAVA_HOME as the variable name and the JDK installation directory as the variable value.
  7. Select the “Path” variable and click “Edit…”.
  8. Click “New” and add %JAVA_HOME%\bin to the list.
  9. Click “OK” on all dialog boxes.
  10. Restart your command prompt or computer.

Compiling Your Java Program

Once your Java environment is set up, the next step is to compile your Java source code into bytecode. The Java compiler, javac, is responsible for this process. It takes your .java files as input and generates .class files, which contain the bytecode that the Java Virtual Machine (JVM) can execute. Compiling is a critical step because it checks your code for syntax errors and ensures that it conforms to the Java language specifications. Without successful compilation, your program cannot be run. The compiler is quite strict, so even minor errors will prevent compilation.

To compile your Java program, open the command line, navigate to the directory containing your .java file using the cd command, and then execute the command javac YourProgramName.java. If the compilation is successful, a .class file with the same name as your Java class will be created in the same directory. If there are errors, the compiler will display detailed error messages, indicating the line number and the type of error. Carefully review these messages and correct the errors in your code before attempting to compile again. Remember that the filename is case-sensitive.

Featured Snippet: The command javac YourProgramName.java compiles a Java source file named “YourProgramName.java” into bytecode, creating a “YourProgramName.class” file. This .class file contains the instructions that the Java Virtual Machine (JVM) executes. Successful compilation requires the Java Development Kit (JDK) to be installed and configured correctly. Always ensure your code is free of syntax errors before compiling to avoid error messages. The filename is case-sensitive.

Running Your Java Program

After successfully compiling your Java program, you can finally run it using the Java runtime environment. The java command is used to execute the .class file. It launches the JVM and loads the bytecode from your .class file into memory. The JVM then interprets and executes the bytecode, bringing your program to life. Remember, you only need to specify the class name without the .class extension when using the java command. The Java runtime environment is responsible for managing memory, handling exceptions, and interacting with the operating system.

To run a Java program from the command line on Windows, navigate to the directory containing your .class file in the command line and execute the command java YourProgramName. If your program requires command-line arguments, you can pass them after the class name, separated by spaces. For example, java YourProgramName arg1 arg2. The JVM will then pass these arguments to your program’s main method. Make sure that your main method is correctly defined as public static void main(String[] args) to receive the arguments.

Here are some important considerations when running Java programs from the command line:

  • Ensure that the .class file is in the correct directory.
  • Use the correct class name (case-sensitive).
  • Pass command-line arguments if required.
  • Check for any runtime exceptions or errors displayed in the command line.

Troubleshooting Common Issues

Even with careful setup and execution, you might encounter issues when trying to run a Java program from the command line on Windows. One of the most common problems is the “command not found” error, which usually indicates that the JAVA_HOME variable or the Path variable is not configured correctly. Double-check these settings and ensure that they point to the correct directories. Another common issue is the ClassNotFoundException, which means that the JVM cannot find the specified class file. This could be due to an incorrect class name, a missing .class file, or the class file being in the wrong directory.

Another potential problem is related to the classpath. The classpath tells the JVM where to look for class files. If your program depends on external libraries or JAR files, you need to include them in the classpath when running the program. You can do this using the -cp or -classpath option with the java command. For example, java -cp “.;lib/mylibrary.jar” YourProgramName. This tells the JVM to look for class files in the current directory (.) and in the lib/mylibrary.jar file. See Oracle’s documentation on setting the classpath for more details.

Here are some key troubleshooting tips:

  • Verify that JAVA_HOME and Path are correctly configured.
  • Double-check the class name and ensure the .class file exists.
  • Set the classpath correctly if your program depends on external libraries.
  • Use a debugger or logging statements to identify runtime errors.
Infographic here
FAQ ---
Q: How do I check if Java is installed correctly?
A: Open the command line and type java -version. If Java is installed correctly, you should see the Java version information.
Q: What if I get a "javac: command not found" error?
A: This usually means that the JDK is not installed correctly or that the Path variable is not configured to include the JDK's bin directory. Double-check your environment variables.
Q: How do I run a Java program with command-line arguments?
A: After compiling, use the command java YourProgramName arg1 arg2, replacing YourProgramName with your class name and arg1 and arg2 with your arguments.
Mastering the command line execution of Java programs is a significant step in your development journey. It provides you with greater control and understanding of the underlying processes. This knowledge is crucial for tasks such as deploying applications, automating builds, and debugging complex issues. By understanding how to **run a Java program from the command line on Windows**, you're not just executing code; you're gaining a deeper appreciation for the Java ecosystem.

Now that you know how to compile and run your Java programs directly from the command line, you’re well-equipped to tackle more advanced projects. Explore topics like build automation tools (e.g., Maven, Gradle), continuous integration/continuous deployment (CI/CD) pipelines, and scripting for system administration. These skills will further enhance your productivity and open up new possibilities in your development career. Don’t hesitate to experiment and delve deeper into the world of Java development. Check out this helpful resource: Codecademy’s Java course. And remember, continuous learning is key to staying ahead in the ever-evolving world of technology! For more information on Java, visit Java.com.

Question & Answer :
I’m trying to execute a Java program from the command line in Windows.
Here is my code:

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class CopyFile { public static void main(String[] args) { InputStream inStream = null; OutputStream outStream = null; try { File afile = new File("input.txt"); File bfile = new File("inputCopy.txt"); inStream = new FileInputStream(afile); outStream = new FileOutputStream(bfile); byte[] buffer = new byte[1024]; int length; // copy the file content in bytes while ((length = inStream.read(buffer)) > 0) { outStream.write(buffer, 0, length); } inStream.close(); outStream.close(); System.out.println("File is copied successful!"); } catch (IOException e) { e.printStackTrace(); } } } 

I’m not sure how to execute the program - any help?
Is this possible on Windows?
Why is it different than another environment (I thought JVM was write once, run anywhere)?

Source: javaindos.

Let’s say your file is in C:\mywork\

Run Command Prompt

C:\> cd \mywork 

This makes C:\mywork the current directory.

C:\mywork> dir 

This displays the directory contents. You should see filenamehere.java among the files.

C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin 

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java 

This runs javac.exe, the compiler. You should see nothing but the next system prompt…

C:\mywork> dir 

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

C:\mywork> java filenamehere 

This runs the Java interpreter. You should then see your program output.

If the system cannot find javac, check the set path command. If javac runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld command. Java is case-sensitive!