4 minutes read

Programmers use programming languages to write computer programs to get some practical results. Java is one of the most popular and powerful programming languages that is widely used to create a variety of applications, including desktop applications. In order to get the result, you need to write code and compile and run it. In this topic, you will learn how to compile and run the simplest Hello World program on your computer.

Installing Java on your computer

Before compiling and running any Java code, you need to install a JDK to develop Java applications. Please, download the up-to-date Java version. Just follow the installation instructions given for your operating system.

To check that the installation has been completed, let's check the version of Java by typing the following command in a terminal:

java -version

It outputs the version of Java that is installed on your computer. If it does not work correctly, open the installation instructions and try to set the path variable in your operating system.

If you get stuck with Java not being recognizable, try to watch this video.

Writing a program

Let's write a simple program and then start it on your computer. To do that, we will use a terminal.

Step 1. Create a file named Main.java using any text editor (such as TextPad or NotePad++ for Windows; jEdit or gedit for Mac OS X; gedit for Ubuntu; or something else) and save it in a folder.

Step 2. Paste the following source code into this file:

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

The public class name must be the same as the file name.

Note: We assume you already understand how the Hello World program works. If not, see here and then come back.

Compiling and running a program

To run the program, we will use a terminal installed in your OS. All the following commands need to be executed from within the same folder that the .java file is created in.

Step 3. Compile the program using the following command in the terminal:

javac Main.java

The javac command asks the compiler to translate the source code into bytecode. The result of this command is a file named Main.class.

Step 4. Run the compiled program (make sure that your terminal is open in the same directory as your source file):

java -cp . Main

The java command starts a Java application. It does this by starting a JRE and invoking the main method inside the Main class.

The -cp parameter (classpath) specifies the location of user-defined classes and packages. The dot . means the current terminal directory. We will consider it in detail in the next topics.

Note: You should not specify the .class extension when running a program.

The program should output the following text:

Hello, Java

Below is an animation that executes all these steps.

Compiling and running a program

Congratulations! You have just started a simple program on your computer. Try to change this program if you would like to get more interesting results.

Running without explicit compilation

Since Java 11, it is possible to compile and run Java source code file using a single command:

java Main.java

This command compiles the file in-memory and directly runs the program, without creating a .class file on disk.

Many developers don't know this small but interesting feature.

Run multi-file source programs directly

In Java 22, this capability has been expanded! You can now run multi-file source-code programs directly from the command line — no manual compilation or build tool is required.

For example, suppose you have two files in the same folder:

// Prog.java
class Prog {
    public static void main(String[] args) {
        Helper.run();
    }
}

// Helper.java
class Helper {
    static void run() {
        System.out.println("Hello from multiple files!");
    }
}

You can run this program with:

java Prog.java

The Java launcher will automatically find and compile the required source files (Helper.java, in this case) in memory. This makes it easier to experiment with projects that span multiple files, without needing to set up javac, classpaths, or build tools.

This is a new feature in Java 22, introduced as part of JEP 458.

Some notes:

  • Only .java files that are referenced in the code will be compiled.

  • You should avoid declaring the same class in multiple files — the launcher will report an error if it detects duplicates.

This feature helps developers move smoothly from small single-file experiments to more complex multi-file projects — without friction.

Conclusion

We have looked at the process of compiling and running a simple program on a computer. To run a Java application, you need the JDK installed on your computer, with an up-to-date version. Converting the source code to bytecode is done using the javac command. Running a compiled program is done with the java command.

With Java 11, you can compile and run a single Java source file directly using a single command: java Main.java. With Java 22, you can now run multi-file source programs using just one command — making it even easier to build and test small applications without setting up a build process.

Keep learning and improving your skills and you will achieve great things in the world of development.

1555 learners liked this piece of theory. 68 didn't like it. What about you?
Report a typo