jbang
Run Java code from source without setup
TLDR
Initialize a simple Java class
Initialize a Java class (useful for scripting)
Use jshell to explore and use a script and any dependencies in a REPL editor
Setup a temporary project to edit a script in an IDE
Run a Java code snippet (Java 9 and later)
Run command-line application
Install a script on the user's $PATH
Install a specific version of JDK to be used with jbang
SYNOPSIS
jbang [OPTIONS] [run]
jbang
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
) which can generate a temporary project structure (e.g., Maven or Gradle) and open the script in a compatible IDE, providing full IDE features like code completion and debugging, then cleaning up the temporary project upon exit.
HISTORY
JBang was created by Max Rydahl Andersen, a well-known figure in the Java ecosystem. It emerged around late 2019/early 2020 with the aim to simplify the execution of Java code, addressing the perceived complexity of setting up traditional Java projects for simple scripts or command-line tools. Inspired by the ease of use of scripting languages, JBang quickly gained traction within the Java community as a powerful tool for rapid prototyping, developing small utilities, and educational purposes, making Java more accessible for "scripting" use cases. Its continuous development focuses on improving user experience, performance, and integration with the wider Java toolchain.