Creating Executable Java "Scripts" with an Executable Java "Script"

The following Java "script" converts the src/main/java/airhacks/App.java script into an executable "Single-File Source-Code Program"


#!/usr/bin/java --source 17
import java.nio.file.Path;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;

public class CreateJavaScript {

    public static void main(String ...args) throws IOException{
        var scriptDeclaration = "#!/usr/bin/java --source 17";
        var inputFile = Path.of("src/main/java/airhacks/App.java");
        var outputFile = Path.of("target","App.sh");

        var content = Files.readString(inputFile);
        var contentWithDeclaration = """
        $declaration
        $content
        """.replace("$declaration", scriptDeclaration)
            .replace("$content", content);
        Files.writeString(outputFile,contentWithDeclaration,StandardOpenOption.CREATE);
    }
}

This Java "script" ships with Maven Java 17 Quickstarter: https://github.com/AdamBien/java17-plain

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments
License