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 [OPTIONS] [run] [SCRIPT_ARGS...]
jbang [OPTIONS] [arguments...]

Example:
jbang myscript.java Hello World
jbang init --template=cli mycli.java
jbang app --native mycli.java

PARAMETERS

--help, -h
    Display help information for JBang or a specific command.

--version, -v
    Display JBang version information.

--verbose, -V
    Enable verbose output, showing more details about execution.

--debug, -d
    Enable debug output, useful for troubleshooting.

--quiet, -q
    Suppress non-essential output.

--force, -f
    Force an operation, overwriting existing files or ignoring warnings.

--fresh
    Clear cached dependencies and rebuild.

--deps
    (run command) Add additional dependencies directly via command line (e.g., group:artifact:version).

--cp
    (run command) Add additional paths to the classpath.

--main
    (run command) Specify the main class to execute.

--java
    (run command) Specify the Java version to use for execution.

--native
    (app/build command) Build a native executable using GraalVM.

--template=
    (init command) Specify a template to use for new scripts.

--alias=
    (alias command) Specify the name for an alias.

--shell
    (run command) Open a shell with the script's dependencies on the classpath.

DESCRIPTION

JBang is a command-line tool designed to make it easy to run Java, Groovy, and Kotlin code directly from source files, without needing a full build system like Maven or Gradle for simple tasks. It significantly simplifies the Java development experience for scripting, prototyping, and creating small utilities. JBang automatically handles compilation, resolves external dependencies (from Maven Central, local files, or URLs) declared via simple comments in the script, and manages the classpath. It supports running local files, remote URLs, and even pre-defined aliases. Beyond just running scripts, JBang offers commands to initialize new scripts, edit them in an IDE, build distributable JARs, or even compile them into native executables using GraalVM for fast startup and smaller footprint. Its goal is to bring the convenience of dynamic language scripting to the robust and performant Java ecosystem.

CAVEATS


- JDK Requirement: JBang requires a Java Development Kit (JDK) to be installed. While it can often manage and download JDKs, a pre-installed JDK is recommended.
- Initial Performance: The first run of a script might be slower due to compilation, dependency resolution, and caching. Subsequent runs are typically much faster.
- Native Image Building: Building native executables with GraalVM (--native option) requires GraalVM to be configured and can be resource-intensive in terms of memory and time.
- Security Considerations: When running scripts from untrusted URLs, be aware of potential security risks. JBang provides a trust command to manage trusted sources.

SHEBANG SUPPORT

JBang scripts can be made directly executable on Unix-like systems by adding a shebang line (e.g., #!/usr/bin/env jbang) at the top of the script file. This allows scripts to be run directly like myscript.java after making them executable (chmod +x myscript.java).

DEPENDENCY MANAGEMENT

JBang simplifies dependency management through special //DEPS comments directly within the source file (e.g., //DEPS com.google.guava:guava:31.1-jre). It automatically downloads and caches these dependencies from Maven Central or other configured repositories.

ALIASES

JBang supports creating custom aliases for frequently used scripts or complex command combinations. These aliases can be defined globally and allow users to run scripts with a short, memorable name (e.g., jbang alias add hello github.com/foo/bar/hello.java then jbang hello).

IDE INTEGRATION

JBang includes an edit command (jbang edit