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 specify the Java compiler version in a pom.xml file?

When you need to compile a project for a specific Java version you can configure maven compiler plugin to set the source and the target version. The following pom.xml file configuration show you how to do it.

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>    

Invalid value for the source and the target version in our project will make our project compilation process failed. For example when we try to use the diamond operator (<>) which available in Java 7, while the maven compiler plugin is set to version 1.5, can produce compiler error like this:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project servlet-example: Compilation failure
[ERROR] /Users/wsaryada/Studio/kodejava.org/webapp-example/servlet-example/src/main/java/org/kodejava/example/servlet/SessionCounter.java:[10,51] diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)

How do I programmatically compile Java class?

This example using the Java Compiler API introduced in JDK 1.6 to programmatically compile a Java class. Here we’ll compile the Hello.java. The process of compiling can be start by obtaining a JavaCompiler from the ToolProvider.getSystemJavaCompiler().

The simplest way to compile is by calling the run() method of the compiler and passing the first three arguments with null value. These three argument will use the default System.in, System.out and System.err. The final parameter is the file of the Java class to be compiled.

When error happened during compilation process the non-zero result code will be returned. After the compile process you’ll have the Hello.class just as if you were compiling using the javac command.

package org.kodejava.tools;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class CompileHello {
    public static void main(String[] args) {
        System.out.println(System.getProperty("user.dir"));
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int result = compiler.run(null, null, null,
                "kodejava-tools/src/main/java/org/kodejava/tools/Hello.java");

        System.out.println("Compile result code = " + result);
    }
}