How to Run Java Programs From Command Line

Aryan Tyagi Mar 11, 2025 Java
  1. Setting Up Your Environment
  2. Writing Your First Java Program
  3. Compiling the Java Program
  4. Running the Java Program
  5. Troubleshooting Common Issues
  6. Conclusion
  7. FAQ
How to Run Java Programs From Command Line

Running Java programs from the command line is an essential skill for developers, whether you’re a beginner or an experienced programmer.

This tutorial will guide you through the steps to execute Java programs directly from your command line interface. By mastering this process, you can streamline your workflow, debug more effectively, and deepen your understanding of how Java operates under the hood. We will cover everything from setting up your environment to compiling and running your Java code, ensuring that you have all the tools you need at your fingertips. Let’s dive in!

Setting Up Your Environment

Before you can run Java programs from the command line, you need to ensure that you have the Java Development Kit (JDK) installed on your machine. The JDK includes all the necessary tools to compile and run Java applications.

Step 1: Install JDK

  1. Download the JDK from the official Oracle website or OpenJDK.
  2. Follow the installation instructions specific to your operating system (Windows, macOS, or Linux).
  3. After installation, set the environment variables.

For Windows, you will need to add the JDK’s bin directory to your PATH variable. On macOS and Linux, this can usually be done by adding a line to your .bash_profile or .bashrc file.

Step 2: Verify Installation

Open your command line interface and type:

java -version

If installed correctly, you should see the version of Java displayed. This confirms that your environment is set up properly.

Output:

java version "17.0.x" 2023-xx-xx
Java(TM) SE Runtime Environment (build 17.0.x)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.x, mixed mode)

Writing Your First Java Program

Now that your environment is ready, let’s write a simple Java program. Open a text editor and create a file named HelloWorld.java. Here’s a basic example:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

This program defines a class called HelloWorld with a main method that prints “Hello, World!” to the console.

Save the file in a directory where you can easily access it from the command line.

Compiling the Java Program

To run your Java program, you first need to compile it. This converts your Java code into bytecode that the Java Virtual Machine (JVM) can execute.

Navigate to the directory where your HelloWorld.java file is saved using the command line, and run the following command:

javac HelloWorld.java

If there are no errors, this command will create a file named HelloWorld.class. This file contains the bytecode version of your Java program.

Output:

(no output)

If you encounter any errors, double-check your code for typos or syntax issues.

Running the Java Program

Now that your program is compiled, it’s time to run it. Use the following command:

java HelloWorld

This command tells the JVM to execute the HelloWorld class.

Output:

Hello, World!

Congratulations! You’ve just run your first Java program from the command line. This simple process illustrates the core workflow of writing, compiling, and executing Java code.

Troubleshooting Common Issues

When running Java programs from the command line, you might encounter a few common issues. Here are some troubleshooting tips:

  1. Command Not Found: If you receive an error saying javac or java is not recognized, ensure that the JDK is properly installed and the PATH variable is set correctly.

  2. File Not Found: Make sure you are in the correct directory where your .java file is located. Use the cd command to navigate to the right folder.

  3. Compilation Errors: If javac returns errors, read them carefully. They often indicate where the problem lies in your code.

  4. Class Not Found: If you see an error stating that the class cannot be found, check that you are using the correct class name and that it matches the filename.

By familiarizing yourself with these common issues, you can troubleshoot effectively and keep your development process smooth.

Conclusion

Running Java programs from the command line is a fundamental skill that every Java developer should master. By following the steps outlined in this tutorial, you can easily write, compile, and execute Java code. This knowledge not only enhances your programming skills but also helps you understand the inner workings of Java applications. So, get out there, write some code, and enjoy the process of learning Java through the command line!

FAQ

  1. How do I check if Java is installed on my system?
    You can check if Java is installed by typing java -version in your command line. If it’s installed, you’ll see the version number.

  2. What should I do if I encounter a compilation error?
    Review the error message provided by the javac command. It will usually indicate the line number and type of error.

  3. Can I run Java programs without the command line?
    Yes, you can run Java programs using integrated development environments (IDEs) like Eclipse or IntelliJ IDEA, which provide a graphical interface.

  4. What is the difference between javac and java commands?
    javac is used to compile Java source files into bytecode, while java is used to run the compiled bytecode.

  5. Is it necessary to set environment variables for Java?
    Setting environment variables is essential for the command line to recognize Java commands.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe