How to compile and run Java 17 code using command line

To compile and run Java 17 code using the command line, follow these steps:


1. Install Java 17

  • Ensure that Java 17 is installed on your system.
  • Run the following command to check the installed Java version:
java -version

If Java 17 is not installed, download and install it from the official Oracle website or use OpenJDK.


2. Write Your Java Code

  • Create a Java file with the .java extension. For example, create a file named HelloWorld.java with the following content:
public class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello, World!");
   }
}

3. Open Command Line

  • Open a terminal (on Linux/Mac) or Command Prompt/PowerShell (on Windows).

4. Navigate to the Directory

  • Go to the directory where the .java file is located using the cd command. For example:
cd /path/to/your/code

5. Compile the Java File

  • Use the javac command to compile the .java file into bytecode. The javac compiler will create a .class file.
javac HelloWorld.java
  • If there are no errors, you’ll see a file named HelloWorld.class in your directory.

6. Run the Compiled Java File

  • Execute the compiled .class file using the java command (without the .class extension):
java HelloWorld
  • You should see the following output:
Hello, World!

7. (Optional) Use Java 17 Specific Features

  • Java 17 brought several new features such as sealed classes, pattern matching for switch, and more. Make sure your code uses features specific to Java 17 to fully utilize it.

Common Troubleshooting

  1. 'javac' is not recognized as an internal or external command:
    • Ensure Java is added to your system’s PATH environment variable. Refer to your operating system’s documentation to add the Java bin directory to the PATH.
  2. Specify Java Version (if multiple versions are installed):
    • Use the full path to the desired Java version for compilation and execution:
/path/to/java17/bin/javac HelloWorld.java
/path/to/java17/bin/java HelloWorld

With these steps, your Java 17 code should successfully compile and run from the command line.

How do I write Hello World program in Java?

The Hello World program is a classic example to start with when we begin to learn a new programming language. The main purpose is to know the basic syntax of the language. Below is the Java version of a Hello World program, it is simple enough to start.

package org.kodejava.basic;

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

The code contains one class called HelloWorld, we declare a class using the class keyword. A main(String[] args) method with a public static void signature and an argument of string array is a special method in Java class. This method is the execution entry point of every Java application. When a class have this method it will be executable. And finally in the body of the method we have a single line of code that write a Hello World string to the console.

The HelloWorld class must be saved in a file named HelloWorld.java, the class file name is case-sensitive. In the example we also define a package name for the class. In this case the HelloWorld.java must be placed in a org\kodejava\basic directory.

To run the application we need to compile it first. I assume that you have your Java in your path. To compile it type:

javac -cp . org.kodejava.basic.HelloWorld.java

The compilation process will result a file called HelloWorld.class, this is the binary version of our program. As you can see that the file ends with .class extension because Java is everything about class. To run it type the command bellow, class name is written without extension.

java -cp . org.kodejava.basic.HelloWorld

And you will see a string Hello World! is printed out in the console as the output of your first Java program, the HelloWorld program.

To install Java Development Kit (JDK) and configure Java environment you can see the following tutorial: