Running an application distributed as an all-in-one Jar

Running an application distributed as an all-in-one Jar#

This example shows how to use the cjdk.cache_file() function to download an application Jar and run it with the desired JDK.

We will use the Checkstyle program (a Java linter) as an example.

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)
checkstyle_url = "https://github.com/checkstyle/checkstyle/releases/download/checkstyle-10.3.1/checkstyle-10.3.1-all.jar"

The following will download the Jar the first time it is run, and place it in the cjdk cache directory.

checkstyle_path = cjdk.cache_file(
    "Checkstyle", checkstyle_url, "checkstyle-all.jar"
)
cjdk: Installing Checkstyle to /home/runner/.cache/cjdk

Now we will run Checkstyle, with a JDK that is downloaded if needed.

with cjdk.java_env(vendor="temurin-jre", version="17.0.3"):
    subprocess.run(
        [
            "java",
            "-jar", str(checkstyle_path),
            "-c", "/google_checks.xml",
            "Hello.java",
        ]
    )
Starting audit...
[WARN] /tmp/tmpxhlerz01/Hello.java:2:1: Missing a Javadoc comment. [MissingJavadocType]
[WARN] /tmp/tmpxhlerz01/Hello.java:3:5: 'method def modifier' has incorrect indentation level 4, expected level should be 2. [Indentation]
[WARN] /tmp/tmpxhlerz01/Hello.java:4:9: 'method def' child has incorrect indentation level 8, expected level should be 4. [Indentation]
[WARN] /tmp/tmpxhlerz01/Hello.java:5:5: 'method def rcurly' has incorrect indentation level 4, expected level should be 2. [Indentation]
Audit done.

Checkstyle has pointed out that our example code is missing documentation comments and does not conform to the indentation rules defined in google_checks.xml (which is included in the Checkstyle Jar).