Add jbang: single-file Java with JBang 0.132.1 on JDK 25 (deps, compact source files, Spring Boot, templates, native)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6ZEVHGaYy15HAxRzy
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
7c677a39d2
commit
ddc3ec9d90
@@ -0,0 +1,10 @@
|
||||
//JAVA 25+
|
||||
|
||||
// A compact source file (JEP 512, final in Java 25): no class, no "public static",
|
||||
// no String[] args. The compiler wraps this in an implicit class named after the file.
|
||||
String greeting = "Hello from a compact source file";
|
||||
|
||||
void main() {
|
||||
IO.println(greeting);
|
||||
IO.println("Implicit class: " + getClass().getName());
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
//JAVA 25+
|
||||
//DEPS info.picocli:picocli:4.7.7
|
||||
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.Command;
|
||||
import picocli.CommandLine.Option;
|
||||
import picocli.CommandLine.Parameters;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* A real command line tool in one file: argument parsing, --help and --version for free.
|
||||
* The only build file is the //DEPS line above.
|
||||
*/
|
||||
@Command(name = "greeter", mixinStandardHelpOptions = true, version = "greeter 1.0",
|
||||
description = "Greets people, optionally loudly.")
|
||||
public class Greeter implements Callable<Integer> {
|
||||
|
||||
@Parameters(index = "0", description = "Who to greet.")
|
||||
String name;
|
||||
|
||||
@Option(names = {"-l", "--loud"}, description = "Shout the greeting.")
|
||||
boolean loud;
|
||||
|
||||
@Option(names = {"-n", "--times"}, defaultValue = "1", description = "Repeat count (default: ${DEFAULT-VALUE}).")
|
||||
int times;
|
||||
|
||||
@Override
|
||||
public Integer call() {
|
||||
String msg = "Hello, " + name + "!";
|
||||
for (int i = 0; i < times; i++) {
|
||||
System.out.println(loud ? msg.toUpperCase() : msg);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.exit(new CommandLine(new Greeter()).execute(args));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
///usr/bin/env jbang "$0" "$@" ; exit $?
|
||||
//JAVA 25+
|
||||
|
||||
/**
|
||||
* The classic shape: one file, one class, a static main.
|
||||
* The first line is a shebang trick so ./Hello.java works on Linux/macOS once chmod +x.
|
||||
*/
|
||||
public class Hello {
|
||||
public static void main(String[] args) {
|
||||
String who = args.length > 0 ? String.join(" ", args) : "world";
|
||||
System.out.println("Hello, " + who + "!");
|
||||
System.out.println("Running on Java " + Runtime.version().feature()
|
||||
+ " (" + System.getProperty("java.vendor") + ")");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
//JAVA 25+
|
||||
//DEPS tools.jackson.core:jackson-databind:3.3.0
|
||||
|
||||
import tools.jackson.databind.SerializationFeature;
|
||||
import tools.jackson.databind.json.JsonMapper;
|
||||
|
||||
/** Reads JSON on stdin, writes it back pretty-printed. A tiny jq-lite in one file. */
|
||||
public class Pretty {
|
||||
public static void main(String[] args) throws Exception {
|
||||
var mapper = JsonMapper.builder().enable(SerializationFeature.INDENT_OUTPUT).build();
|
||||
var tree = mapper.readTree(System.in);
|
||||
System.out.println(mapper.writeValueAsString(tree));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
# jbang — run Java like a script
|
||||
|
||||
Companion module for the ankurm.com post **JBang: Run Java Like a Script in 2026**.
|
||||
Everything the post explains lives in this directory; every console block in the post is quoted
|
||||
from a file in [`output/`](output/), regenerated by one command.
|
||||
|
||||
## Versions tested
|
||||
|
||||
| Component | Version | Note |
|
||||
|---|---|---|
|
||||
| JDK | Temurin 25.0.5.1 (LTS) | compact source files (JEP 512) are final in 25 |
|
||||
| JBang | 0.132.1 | latest jar published to Maven Central; GitHub's newest stable is v0.143.0 (not run here) |
|
||||
| picocli | 4.7.7 | |
|
||||
| Jackson | 3.3.0 (`tools.jackson`) | |
|
||||
| Spring Boot | 4.2.0 (`spring-boot-starter-webmvc`) | Tomcat 11.0.27 |
|
||||
| GraalVM | Oracle GraalVM 25.0.4 | `native-image` for the `--native` step |
|
||||
|
||||
## Layout
|
||||
|
||||
| File | What it demonstrates |
|
||||
|---|---|
|
||||
| `Hello.java` | the smallest script; shebang line; cold vs warm start |
|
||||
| `CompactHello.java` | a compact source file (`void main()`), no class |
|
||||
| `Greeter.java` | `//DEPS` + picocli: a real CLI with `--help` |
|
||||
| `Pretty.java` | Jackson 3 JSON pretty-printer reading stdin |
|
||||
| `multi/` | `//SOURCES` and `//FILES` |
|
||||
| `SpringApp.java` | a whole Spring Boot web app in one file (BOM via `@pom`) |
|
||||
| `broken/` | four failures, kept outside the happy path on purpose |
|
||||
| `templates/tool.java.qute` + `jbang-catalog.json` | a custom `jbang init` template and aliases |
|
||||
| `scripts/jb.sh` | runs JBang (real launcher if installed, otherwise the Maven Central jar) |
|
||||
| `scripts/run-all.sh` | regenerates every file in `output/` |
|
||||
|
||||
## Run it
|
||||
|
||||
```bash
|
||||
export JAVA_HOME=/path/to/jdk-25 PATH=$JAVA_HOME/bin:$PATH
|
||||
jbang Hello.java Ankur
|
||||
jbang Greeter.java --help
|
||||
jbang SpringApp.java # then: curl localhost:8089/hello
|
||||
./scripts/run-all.sh # rebuild output/*.txt
|
||||
```
|
||||
|
||||
No `docs/` folder on purpose: the explanation is in the blog post.
|
||||
@@ -0,0 +1,29 @@
|
||||
//JAVA 25+
|
||||
//DEPS org.springframework.boot:spring-boot-dependencies:4.2.0@pom
|
||||
//DEPS org.springframework.boot:spring-boot-starter-webmvc
|
||||
//RUNTIME_OPTIONS -Dserver.port=8089 -Dspring.main.banner-mode=off
|
||||
|
||||
package demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* A whole Spring Boot web application in a single file. The BOM (@pom) pins every
|
||||
* Spring Boot dependency, so the starter needs no version of its own.
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
public class SpringApp {
|
||||
|
||||
@GetMapping("/hello")
|
||||
String hello() {
|
||||
return "Hello from a single-file Spring Boot app";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringApp.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
//JAVA 21
|
||||
void main() {
|
||||
System.out.println("compact source file on Java 21");
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//JAVA 25+
|
||||
//DEPS info.picocli:picocli:9.9.9
|
||||
|
||||
public class MissingDep {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("never reached");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
//JAVA 99
|
||||
public class NeedsJava99 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("never reached");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//JAVA 25+
|
||||
//DEPS org.springframework.boot:spring-boot-dependencies:4.2.0@pom
|
||||
//DEPS org.springframework.boot:spring-boot-starter-webmvc
|
||||
//RUNTIME_OPTIONS -Dserver.port=8090 -Dspring.main.banner-mode=off
|
||||
|
||||
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* A whole Spring Boot web application in a single file. The BOM (@pom) pins every
|
||||
* Spring Boot dependency, so the starter needs no version of its own.
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
public class SpringDefaultPackage {
|
||||
|
||||
@GetMapping("/hello")
|
||||
String hello() {
|
||||
return "Hello from a single-file Spring Boot app";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringDefaultPackage.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"aliases": {
|
||||
"greeter": {
|
||||
"script-ref": "Greeter.java",
|
||||
"description": "Picocli greeter CLI"
|
||||
},
|
||||
"pretty": {
|
||||
"script-ref": "Pretty.java",
|
||||
"description": "Pretty-print JSON from stdin (Jackson 3)"
|
||||
}
|
||||
},
|
||||
"templates": {
|
||||
"tool": {
|
||||
"file-refs": {
|
||||
"{basename}.java": "templates/tool.java.qute"
|
||||
},
|
||||
"description": "Picocli CLI skeleton",
|
||||
"properties": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
//JAVA 25+
|
||||
//SOURCES Shout.java
|
||||
//FILES banner.txt=banner.txt
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* More than one file: //SOURCES pulls in Shout.java, //FILES makes banner.txt
|
||||
* available on the classpath as a resource.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try (InputStream in = Main.class.getClassLoader().getResourceAsStream("banner.txt")) {
|
||||
System.out.print(new String(in.readAllBytes()));
|
||||
}
|
||||
System.out.println(Shout.shout("one script, two sources, one resource"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/** A helper class living beside Main.java; JBang compiles it because Main lists it in //SOURCES. */
|
||||
public class Shout {
|
||||
static String shout(String s) {
|
||||
return s.toUpperCase() + "!";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
=== multi-file JBang demo ===
|
||||
@@ -0,0 +1,34 @@
|
||||
# Cold run (empty JBang cache) then warm run, same script
|
||||
$ jbang Hello.java Ankur
|
||||
[jbang] Building jar for Hello.java...
|
||||
Hello, Ankur!
|
||||
Running on Java 25 (Eclipse Adoptium)
|
||||
[wall clock: 5.745s]
|
||||
|
||||
$ jbang Hello.java Ankur
|
||||
Hello, Ankur!
|
||||
Running on Java 25 (Eclipse Adoptium)
|
||||
[wall clock: 2.338s]
|
||||
|
||||
$ jbang info tools Hello.java
|
||||
{
|
||||
"originalResource": "Hello.java",
|
||||
"backingResource": "/tmp/work/jbang/Hello.java",
|
||||
"applicationJar": "/tmp/tmp.RXxYNQBWOn/cache/jars/Hello.java.af0224835723899ff208d77a8e96fa9f8c064e617b7dbc225df29b5021aab8a0/Hello.jar",
|
||||
"mainClass": "Hello",
|
||||
"resolvedDependencies": [],
|
||||
"javaVersion": "25",
|
||||
"requestedJavaVersion": "25+",
|
||||
"availableJdkPath": "/tmp/tools/jdk-25.0.5.1+2",
|
||||
"compileOptions": [
|
||||
"-g",
|
||||
"-parameters"
|
||||
],
|
||||
"sources": [
|
||||
{
|
||||
"originalResource": "Hello.java",
|
||||
"backingResource": "/tmp/work/jbang/Hello.java"
|
||||
}
|
||||
],
|
||||
"docs": {}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
$ jbang CompactHello.java
|
||||
[jbang] Building jar for CompactHello.java...
|
||||
Hello from a compact source file
|
||||
Implicit class: CompactHello
|
||||
@@ -0,0 +1,19 @@
|
||||
$ jbang Greeter.java --help
|
||||
[jbang] Resolving dependencies...
|
||||
[jbang] info.picocli:picocli:4.7.7
|
||||
[jbang] Dependencies resolved
|
||||
[jbang] Building jar for Greeter.java...
|
||||
Usage: greeter [-hlV] [-n=<times>] <name>
|
||||
Greets people, optionally loudly.
|
||||
<name> Who to greet.
|
||||
-h, --help Show this help message and exit.
|
||||
-l, --loud Shout the greeting.
|
||||
-n, --times=<times> Repeat count (default: 1).
|
||||
-V, --version Print version information and exit.
|
||||
|
||||
$ jbang Greeter.java --loud --times 2 Ankur
|
||||
HELLO, ANKUR!
|
||||
HELLO, ANKUR!
|
||||
|
||||
$ jbang info classpath Greeter.java
|
||||
/tmp/tmp.RXxYNQBWOn/cache/jars/Greeter.java.5779a46c55f3fbe373ce3efdcca5ec2bb019e2179eee9b52e6fc7d76d8acbec9/Greeter.jar:/root/.m2/repository/info/picocli/picocli/4.7.7/picocli-4.7.7.jar
|
||||
@@ -0,0 +1,11 @@
|
||||
$ echo '{"a":1,"b":[1,2,{"c":null}]}' | jbang Pretty.java
|
||||
[jbang] Resolving dependencies...
|
||||
[jbang] tools.jackson.core:jackson-databind:3.3.0
|
||||
[jbang] Dependencies resolved
|
||||
[jbang] Building jar for Pretty.java...
|
||||
{
|
||||
"a" : 1,
|
||||
"b" : [ 1, 2, {
|
||||
"c" : null
|
||||
} ]
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
$ jbang Main.java
|
||||
[jbang] Building jar for Main.java...
|
||||
=== multi-file JBang demo ===
|
||||
ONE SCRIPT, TWO SOURCES, ONE RESOURCE!
|
||||
@@ -0,0 +1,8 @@
|
||||
$ jbang SpringApp.java
|
||||
[log lines that matter, timestamps/PIDs trimmed]
|
||||
INFO demo.SpringApp : Starting SpringApp using Java 25.0.5.1 with PID 22275 (/tmp/tmp.RXxYNQBWOn/cache/jars/SpringApp.java.3f7221da265b2f6554670ab927989c9962b804151b313b88dc6e785f1aba42b3/SpringApp.jar started by root in /tmp/work
|
||||
INFO o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 8089 (http) with context path '/'
|
||||
INFO demo.SpringApp : Started SpringApp in 4.875 seconds (process running for 5.977)
|
||||
|
||||
$ curl -s localhost:8089/hello
|
||||
Hello from a single-file Spring Boot app
|
||||
@@ -0,0 +1,8 @@
|
||||
$ jbang broken/SpringDefaultPackage.java
|
||||
[log lines that matter, timestamps/PIDs trimmed]
|
||||
INFO SpringDefaultPackage : Starting SpringDefaultPackage using Java 25.0.5.1 with PID 22621 (/tmp/tmp.RXxYNQBWOn/cache/jars/SpringDefaultPackage.java.51714cd16af051bf7eb9f5a0f0d4aeb0f3bd08b4e81750139ad135d9e660ca37/SpringDefaultP
|
||||
** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
|
||||
APPLICATION FAILED TO START
|
||||
The bean 'errorPageRegistrarBeanPostProcessor', defined in class path resource [org/springframework/boot/web/servlet/support/ErrorPageFilterConfiguration.class], could not be registered. A bean with that name has already been defi
|
||||
|
||||
(no HTTP answer on port 8090 after 120 s)
|
||||
@@ -0,0 +1,13 @@
|
||||
$ jbang broken/CompactNoJava25.java
|
||||
[jbang] Downloading JDK 21. Be patient, this can take several minutes...
|
||||
[jbang] Installing JDK 21...
|
||||
[jbang] Default JDK set to 21 (21.0.13.1+2-LTS [fixed], 21-jbang, /tmp/tmp.RXxYNQBWOn/cache/jdks/21, [Ga, Jdk]))
|
||||
[jbang] Building jar for CompactNoJava25.java...
|
||||
/tmp/work/jbang/broken/CompactNoJava25.java:2: error: unnamed classes are a preview feature and are disabled by default.
|
||||
void main() {
|
||||
^
|
||||
(use --enable-preview to enable unnamed classes)
|
||||
1 error
|
||||
[jbang] [ERROR] Error during compile
|
||||
[jbang] Run with --verbose for more details. The --verbose must be placed before the jbang command. I.e. jbang --verbose run [...]
|
||||
[exit status: 1]
|
||||
@@ -0,0 +1,6 @@
|
||||
$ jbang broken/MissingDep.java
|
||||
[jbang] Resolving dependencies...
|
||||
[jbang] info.picocli:picocli:9.9.9
|
||||
[jbang] [ERROR] Could not read artifact descriptor for info.picocli:picocli:jar:9.9.9
|
||||
[jbang] Run with --verbose for more details. The --verbose must be placed before the jbang command. I.e. jbang --verbose run [...]
|
||||
[exit status: 1]
|
||||
@@ -0,0 +1,4 @@
|
||||
$ jbang broken/NeedsJava99.java
|
||||
[jbang] [ERROR] No suitable JDK was found for requested version: 99
|
||||
[jbang] Run with --verbose for more details. The --verbose must be placed before the jbang command. I.e. jbang --verbose run [...]
|
||||
[exit status: 4]
|
||||
@@ -0,0 +1,26 @@
|
||||
$ jbang template add -f jbang-catalog.json --name tool '{basename}.java=tool.java.qute'
|
||||
[jbang] Template 'tool' added to 'jbang-catalog.json'
|
||||
|
||||
$ cat jbang-catalog.json
|
||||
{
|
||||
"templates": {
|
||||
"tool": {
|
||||
"file-refs": {
|
||||
"{basename}.java": "tool.java.qute"
|
||||
},
|
||||
"description": "Picocli CLI skeleton",
|
||||
"properties": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ jbang init -t tool Slugify.java
|
||||
[jbang] File initialized. You can now run it with 'jbang Slugify.java' or edit it using 'j
|
||||
|
||||
$ sed -n 9,10p Slugify.java
|
||||
|
||||
@Command(name = "Slugify", mixinStandardHelpOptions = true, version = "Slugify 0.1")
|
||||
|
||||
$ jbang Slugify.java Hello World
|
||||
[jbang] Building jar for Slugify.java...
|
||||
Slugify processed: Hello World
|
||||
@@ -0,0 +1,11 @@
|
||||
$ jbang alias list
|
||||
greeter
|
||||
Picocli greeter CLI
|
||||
/tmp/work/jbang/Greeter.java
|
||||
pretty
|
||||
Pretty-print JSON from stdin (Jackson 3)
|
||||
/tmp/work/jbang/Pretty.java
|
||||
|
||||
$ jbang greeter -n 2 Ankur
|
||||
Hello, Ankur!
|
||||
Hello, Ankur!
|
||||
@@ -0,0 +1,10 @@
|
||||
$ jbang export portable -O /tmp/greeter-portable.jar Greeter.java
|
||||
[jbang] Updating jar...
|
||||
[jbang] Exported to /tmp/greeter-portable.jar
|
||||
|
||||
$ ls greeter-portable*
|
||||
greeter-portable.jar
|
||||
|
||||
$ java -jar greeter-portable.jar Ankur
|
||||
Hello, Ankur!
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
# native-image 25.0.4 2026-07-21
|
||||
$ jbang --native Hello.java Native
|
||||
[jbang] Building jar for Hello.java...
|
||||
[jbang] log: /tmp/jbang18248476024474165891native-image
|
||||
Hello, Native!
|
||||
Running on Java 25 (Oracle Corporation)
|
||||
[wall clock: 154.486s]
|
||||
|
||||
$ jbang --native Hello.java Native # second run, binary cached
|
||||
Hello, Native!
|
||||
Running on Java 25 (Oracle Corporation)
|
||||
[wall clock: 1.350s]
|
||||
|
||||
$ ls -la $JBANG_DIR/cache/jars/Hello.java.*/
|
||||
7696
|
||||
4096 .
|
||||
4096 ..
|
||||
7866632 Hello.bin
|
||||
1895 Hello.jar
|
||||
|
||||
$ Hello.bin Native # the cached binary, no JBang, no JVM
|
||||
Hello, Native!
|
||||
Running on Java 25 (Oracle Corporation)
|
||||
[wall clock: 0.005s]
|
||||
|
||||
$ java -cp Hello.jar Hello Native # same program on the JVM
|
||||
Hello, Native!
|
||||
Running on Java 25 (Oracle Corporation)
|
||||
[wall clock: 0.277s]
|
||||
@@ -0,0 +1,37 @@
|
||||
$ jbang https://ankurm.com/wp-content/uploads/2026/12/Hello.java Remote
|
||||
[jbang] https://ankurm.com/wp-content/uploads/2026/12/Hello.java is not from a trusted source thus not running it automatically.
|
||||
|
||||
If you trust the url to be safe to run you can do one of the following
|
||||
|
||||
(1) Trust once: Add no trust, only allow access to this URL for the duration of this run
|
||||
(2) Trust limited url in future: https://ankurm.com/wp-content/uploads/2026/12/
|
||||
(0) Cancel
|
||||
|
||||
[jbang] Type in your choice and hit enter. Will automatically select option (0) after 30 seconds.
|
||||
[jbang] Timeout reached, selecting option (0)
|
||||
[jbang] [ERROR] https://ankurm.com/wp-content/uploads/2026/12/Hello.java is not from a trusted source and user did not confirm trust thus aborting.
|
||||
If you trust the url to be safe to run are here a few suggestions:
|
||||
Limited trust:
|
||||
jbang trust add https://ankurm.com/wp-content/uploads/2026/12/
|
||||
Trust all subdomains:
|
||||
jbang trust add *.ankurm.com
|
||||
Trust all sources (WARNING! disables url protection):
|
||||
jbang trust add *
|
||||
|
||||
For more control edit ~/.jbang/trusted-sources.json
|
||||
|
||||
[jbang] Run with --verbose for more details. The --verbose must be placed before the jbang command. I.e. jbang --verbose run [...]
|
||||
[exit status: 10]
|
||||
|
||||
$ jbang trust add https://ankurm.com/wp-content/uploads/
|
||||
[jbang] Trusting permanently: [https://ankurm.com/wp-content/uploads/]
|
||||
|
||||
$ jbang https://ankurm.com/wp-content/uploads/2026/12/Hello.java Remote
|
||||
[jbang] Building jar for Hello.java...
|
||||
Hello, Remote!
|
||||
Running on Java 25 (Eclipse Adoptium)
|
||||
|
||||
$ grep -v '^//' $JBANG_DIR/trusted-sources.json
|
||||
[
|
||||
"https://ankurm.com/wp-content/uploads/"
|
||||
]
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env bash
|
||||
# Runs JBang. Uses a real `jbang` launcher on PATH if there is one; otherwise emulates the
|
||||
# launcher around the jbang-cli fat jar from Maven Central.
|
||||
#
|
||||
# Why the emulation is needed: the jar does not run your script itself. It prints the final
|
||||
# `java ...` command line on stdout and exits with status 255, and the `jbang` shell launcher
|
||||
# evals that line. This script does the same.
|
||||
#
|
||||
# Environment noise ("Picked up JAVA_TOOL_OPTIONS ...") is stripped from stderr.
|
||||
JBANG_JAR="${JBANG_JAR:-/tmp/jb/jbang-cli-0.132.1-all.jar}"
|
||||
|
||||
if command -v jbang >/dev/null 2>&1; then
|
||||
exec jbang "$@"
|
||||
fi
|
||||
|
||||
out=$(java -jar "$JBANG_JAR" "$@" 2> >(grep -v '^Picked up' >&2))
|
||||
rc=$?
|
||||
if [ "$rc" -eq 255 ]; then
|
||||
eval "$out" 2> >(grep -v '^Picked up' >&2)
|
||||
exit $?
|
||||
fi
|
||||
[ -n "$out" ] && printf '%s\n' "$out"
|
||||
exit "$rc"
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
# Runs a script straight from a URL: first untrusted (JBang refuses), then after `jbang trust add`.
|
||||
# Uses its own empty JBANG_DIR so the trust list starts empty.
|
||||
cd "$(dirname "$0")/.."
|
||||
export JAVA_HOME="${JAVA25:-/tmp/tools/jdk-25.0.5.1+2}" PATH="${JAVA25:-/tmp/tools/jdk-25.0.5.1+2}/bin:$PATH"
|
||||
export JBANG_DIR="$(mktemp -d)"
|
||||
J="$PWD/scripts/jb.sh"
|
||||
U=https://ankurm.com/wp-content/uploads/2026/12/Hello.java
|
||||
step() { printf '$ %s\n' "$*"; }
|
||||
step jbang $U Remote
|
||||
timeout 100 $J $U Remote < /dev/null 2>&1; echo "[exit status: $?]"
|
||||
echo; step jbang trust add https://ankurm.com/wp-content/uploads/
|
||||
$J trust add https://ankurm.com/wp-content/uploads/ 2>&1
|
||||
echo; step jbang $U Remote
|
||||
timeout 100 $J $U Remote < /dev/null 2>&1
|
||||
echo; step "grep -v '^//' \$JBANG_DIR/trusted-sources.json"
|
||||
grep -v '^//' "$JBANG_DIR/trusted-sources.json"
|
||||
Executable
+105
@@ -0,0 +1,105 @@
|
||||
#!/usr/bin/env bash
|
||||
# Regenerates every file in output/. Every console block in the blog post is quoted from here.
|
||||
#
|
||||
# JAVA25=/path/to/jdk-25 GRAAL=/path/to/graalvm-jdk-25 ./scripts/run-all.sh
|
||||
#
|
||||
# Needs: JDK 25, network access to Maven Central, a jbang launcher on PATH *or* the
|
||||
# jbang-cli fat jar (JBANG_JAR). GRAAL is optional; without it the native step is skipped.
|
||||
set -u
|
||||
cd "$(dirname "$0")/.."
|
||||
ROOT=$PWD
|
||||
JAVA25="${JAVA25:-/tmp/tools/jdk-25.0.5.1+2}"
|
||||
GRAAL="${GRAAL:-/tmp/tools/graalvm-jdk-25.0.4+7.1}"
|
||||
export JAVA_HOME="$JAVA25" PATH="$JAVA25/bin:$PATH"
|
||||
export JBANG_DIR="$(mktemp -d)" # fresh cache => the "cold" numbers are really cold
|
||||
J="$ROOT/scripts/jb.sh"
|
||||
OUT="$ROOT/output"; mkdir -p "$OUT"
|
||||
TIMEFORMAT=' [wall clock: %3Rs]'
|
||||
|
||||
step() { printf '$ %s\n' "$*"; }
|
||||
|
||||
{ echo "# Cold run (empty JBang cache) then warm run, same script"
|
||||
step jbang Hello.java Ankur; time $J Hello.java Ankur 2>&1
|
||||
echo; step jbang Hello.java Ankur; time $J Hello.java Ankur 2>&1
|
||||
echo; step jbang info tools Hello.java; $J info tools Hello.java 2>&1
|
||||
} > "$OUT/01-hello.txt" 2>&1
|
||||
|
||||
{ step jbang CompactHello.java; $J CompactHello.java 2>&1; } > "$OUT/02-compact-source-file.txt" 2>&1
|
||||
|
||||
{ step jbang Greeter.java --help; $J Greeter.java --help 2>&1
|
||||
echo; step jbang Greeter.java --loud --times 2 Ankur; $J Greeter.java --loud --times 2 Ankur 2>&1
|
||||
echo; step jbang info classpath Greeter.java; $J info classpath Greeter.java 2>&1
|
||||
} > "$OUT/03-picocli-greeter.txt" 2>&1
|
||||
|
||||
{ step "echo '{\"a\":1,\"b\":[1,2,{\"c\":null}]}' | jbang Pretty.java"
|
||||
echo '{"a":1,"b":[1,2,{"c":null}]}' | $J Pretty.java 2>&1; } > "$OUT/04-jackson-pretty.txt" 2>&1
|
||||
|
||||
{ cd multi; step jbang Main.java; $J Main.java 2>&1; cd ..; } > "$OUT/05-multi-source.txt" 2>&1
|
||||
|
||||
run_spring() { # $1 script $2 port $3 main-class-grep $4 outfile
|
||||
setsid nohup "$J" "$1" > "$ROOT/.spring.log" 2>&1 < /dev/null &
|
||||
local ok=0
|
||||
for i in $(seq 1 60); do
|
||||
if curl -s -m 2 "localhost:$2/hello" > "$ROOT/.hello.txt" 2>/dev/null; then ok=1; break; fi
|
||||
sleep 2
|
||||
done
|
||||
{ step jbang "$1"
|
||||
echo "[log lines that matter, timestamps/PIDs trimmed]"
|
||||
grep -E 'WARNING|Tomcat started|Started |APPLICATION FAILED|could not be registered|Starting .* using' "$ROOT/.spring.log" \
|
||||
| sed -E 's/^[0-9T:.+-]+ +(INFO|WARN|ERROR) [0-9]+ --- \[ *[^]]*\] */\1 /; s/ +: / : /' | cut -c1-230
|
||||
echo
|
||||
if [ $ok = 1 ]; then step curl -s localhost:$2/hello; cat "$ROOT/.hello.txt"; echo
|
||||
else echo "(no HTTP answer on port $2 after 120 s)"; fi
|
||||
} > "$4"
|
||||
for p in $(ps -eo pid,cmd | grep "[$(echo "$3" | cut -c1)]$(echo "$3" | cut -c2-)" | awk '{print $1}'); do kill -9 "$p" 2>/dev/null; done
|
||||
sleep 1; rm -f "$ROOT/.spring.log" "$ROOT/.hello.txt"
|
||||
}
|
||||
run_spring SpringApp.java 8089 'demo.SpringApp' "$OUT/06-spring-boot-single-file.txt"
|
||||
run_spring broken/SpringDefaultPackage.java 8090 'SpringDefaultPackage' "$OUT/07-spring-default-package-failure.txt"
|
||||
|
||||
for f in MissingDep NeedsJava99 CompactNoJava25; do
|
||||
{ step jbang broken/$f.java; $J broken/$f.java 2>&1; echo "[exit status: $?]"; } > "$OUT/08-broken-$f.txt" 2>&1
|
||||
done
|
||||
# exit status of a pipeline-less call (jb.sh returns jbang's status)
|
||||
for f in MissingDep NeedsJava99 CompactNoJava25; do
|
||||
$J broken/$f.java >/dev/null 2>&1; rc=$?
|
||||
sed -i "s/^\[exit status: .*\]/[exit status: $rc]/" "$OUT/08-broken-$f.txt"
|
||||
done
|
||||
|
||||
# Templates and catalog: uses a scratch directory so the committed jbang-catalog.json is the one we ship
|
||||
SCR="$(mktemp -d)"; cp templates/tool.java.qute "$SCR/";
|
||||
{ cd "$SCR"
|
||||
step jbang template add -f jbang-catalog.json --name tool "'{basename}.java=tool.java.qute'"
|
||||
$J template add -f jbang-catalog.json --name tool -d "Picocli CLI skeleton" '{basename}.java=tool.java.qute' 2>&1
|
||||
echo; step cat jbang-catalog.json; cat jbang-catalog.json; echo
|
||||
echo; step jbang init -t tool Slugify.java; $J init -t tool Slugify.java 2>&1 | cut -c1-90
|
||||
echo; step sed -n 9,10p Slugify.java; sed -n 9,10p Slugify.java
|
||||
echo; step jbang Slugify.java "Hello World"; $J Slugify.java "Hello World" 2>&1
|
||||
} > "$OUT/09-templates.txt" 2>&1
|
||||
cd "$ROOT"
|
||||
|
||||
{ step jbang alias list; $J alias list 2>&1
|
||||
echo; step jbang greeter -n 2 Ankur; $J greeter -n 2 Ankur 2>&1
|
||||
} > "$OUT/10-catalog-aliases.txt" 2>&1
|
||||
|
||||
{ step jbang export portable -O /tmp/greeter-portable.jar Greeter.java; $J export portable --force -O /tmp/greeter-portable.jar Greeter.java 2>&1
|
||||
echo; step "ls greeter-portable*"; ls /tmp/greeter-portable* | sed 's#^/tmp/##'
|
||||
echo; step "java -jar greeter-portable.jar Ankur"; java -jar /tmp/greeter-portable.jar Ankur 2>&1 | grep -v '^Picked up'
|
||||
} > "$OUT/11-export-portable.txt" 2>&1
|
||||
|
||||
if [ -x "$GRAAL/bin/native-image" ]; then
|
||||
{ export JAVA_HOME="$GRAAL" PATH="$GRAAL/bin:$PATH"
|
||||
echo "# $(native-image --version 2>&1 | grep -v '^Picked up' | head -1)"
|
||||
step jbang --native Hello.java Native
|
||||
time $J --native Hello.java Native 2>&1 | cut -c1-200
|
||||
echo; step jbang --native Hello.java Native " # second run, binary cached"
|
||||
time $J --native Hello.java Native 2>&1 | cut -c1-200
|
||||
echo; step "ls -la \$JBANG_DIR/cache/jars/Hello.java.*/"; ls -la "$JBANG_DIR"/cache/jars/Hello.java.*/ 2>&1 | awk '{print $5, $NF}'
|
||||
echo; step "Hello.bin Native # the cached binary, no JBang, no JVM"
|
||||
B=$(ls "$JBANG_DIR"/cache/jars/Hello.java.*/Hello.bin); time "$B" Native 2>&1
|
||||
echo; step "java -cp Hello.jar Hello Native # same program on the JVM"
|
||||
time java -cp "$(dirname "$B")/Hello.jar" Hello Native 2>&1 | grep -v '^Picked up'
|
||||
} > "$OUT/12-native.txt" 2>&1
|
||||
else echo "GRAAL not found - skipped native" > "$OUT/12-native.txt"; fi
|
||||
"$ROOT/scripts/remote-run.sh" > "$OUT/13-remote-run-and-trust.txt" 2>&1
|
||||
echo "done; outputs in $OUT"
|
||||
@@ -0,0 +1,25 @@
|
||||
//JAVA 25+
|
||||
//DEPS info.picocli:picocli:4.7.7
|
||||
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.Command;
|
||||
import picocli.CommandLine.Parameters;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
@Command(name = "{baseName}", mixinStandardHelpOptions = true, version = "{baseName} 0.1")
|
||||
public class {baseName} implements Callable<Integer> {
|
||||
|
||||
@Parameters(index = "0", description = "Input to process.")
|
||||
String input;
|
||||
|
||||
@Override
|
||||
public Integer call() {
|
||||
System.out.println("{baseName} processed: " + input);
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.exit(new CommandLine(new {baseName}()).execute(args));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user