LinuxCommandLibrary

jbang

Run Java code from source without setup

TLDR

Initialize a simple Java class

$ jbang init [path/to/file.java]
copy

Initialize a Java class (useful for scripting)
$ jbang init --template=[cli] [path/to/file.java]
copy

Use jshell to explore and use a script and any dependencies in a REPL editor
$ jbang run --interactive
copy

Setup a temporary project to edit a script in an IDE
$ jbang edit --open=[codium|code|eclipse|idea|netbeans|gitpod] [path/to/script.java]
copy

Run a Java code snippet (Java 9 and later)
$ [echo 'Files.list(Paths.get("/etc")).forEach(System.out::println);'] | jbang -
copy

Run command-line application
$ jbang [path/to/file.java] [command] [arg1 arg2 ...]
copy

Install a script on the user's $PATH
$ jbang app install --name [command_name] [path/to/script.java]
copy

Install a specific version of JDK to be used with jbang
$ jbang jdk install [version]
copy

SYNOPSIS

jbang [GLOBAL OPTIONS] [SUBCOMMAND] SCRIPT [-- ARGS]

PARAMETERS

-h, --help
    Show help message

-V, --version
    Print version info

--verbose, -v
    Increase output verbosity

--quiet, -q
    Reduce output

--color={true|false|auto}
    Control colored output

--interactive, -i
    Enable interactive mode

--force
    Force actions like overwrite

--fresh
    Use fresh cache, ignore existing

--no-deps
    Skip dependency resolution

--detect-jdk
    Detect and use project JDK

--trust=SCRIPT
    Trust a script

--edit=SCRIPT
    Edit script with $EDITOR

--app=SCRIPT
    Install as app

init [TEMPLATE]
    Initialize new script

trust SCRIPT
    Trust script permanently

list
    List installed/trusted scripts

delete SCRIPT
    Delete script cache

export --type={jar|native|uber} SCRIPT
    Export to executable

DESCRIPTION

JBang is a versatile command-line tool designed to execute Java code as scripts without requiring build tools like Maven or Gradle. It automatically resolves dependencies from sources like Maven Central, manages Java runtimes via SDKMAN integration, and supports features like templating, packaging into JARs or native executables with GraalVM.

Users can write simple .java files with a shebang (#! /usr/bin/env jbang) for direct execution or run them via jbang Script.java. It excels in rapid prototyping, automation scripts, and sharing code snippets. JBang caches artifacts for speed, trusts scripts selectively for security, and offers subcommands for app management, editing, and exporting.

Perfect for developers wanting script-like simplicity with Java's power, it handles modules, main methods, and args seamlessly, making Java viable for shell scripting.

CAVEATS

Requires Java 8+; initial dependency downloads need internet. Not all Java features (e.g., some reflection) work in native mode. Shebang support varies by shell.

INSTALLATION

curl -s "https://jbang.dev" | bash -s - install
Or brew install jbang on macOS.

EXAMPLE

echo 'System.out.println("Hello " + args[0]);' | jbang run - Hello.java World
Outputs: Hello World

HISTORY

Created by Johan Andersen in 2020 as a Java equivalent to tools like babashka. First public release via GitHub; now at v0.22+. Gained popularity for simplifying Java CLI scripting, with integrations for Adoptium, SDKMAN.

SEE ALSO

java(1), javac(1), sdk(1)

Copied to clipboard