Running Java “Hello, World” from Python

Running Java “Hello, World” from Python#

This example shows how to compile and run Java code.

import cjdk
import subprocess
java_source = """
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
"""
with open("Hello.java", "w") as fp:
    fp.write(java_source)
with cjdk.java_env(vendor="temurin-jre", version="17.0.3"):
    subprocess.run(["javac", "Hello.java"], check=True)
    subprocess.run(["java", "Hello"], check=True)
Hello, World!