For years, developers turned to Python or Bash scripts for automation tasks because Java felt too verbose—requiring full project structures, pom.xml configurations, and manual compilation. JBang completely changes this paradigm by enabling you to write, execute, and package standalone Java scripts instantly.

⚡ Executive Summary / Key Takeaways

  • No Project Boilerplate: Run single-file Java code directly using jbang Script.java.
  • Automatic Dependency Management: Add third-party Maven libraries using //DEPS groupId:artifactId:version comments directly inside Java code.
  • Java 21+ Ready: Specify JVM versions via //JAVA 21 directives without altering your system JDK.
  • Native Compilation: Compile scripts into instant-booting binaries using //NATIVE (via GraalVM).

1. Installing JBang in 10 Seconds

JBang can be installed seamlessly across macOS, Linux, and Windows using standard package managers:

# macOS / Linux (via SDKMAN!)
$ sdk install jbang

# macOS (via Homebrew)
$ brew install jbangdev/tap/jbang

# Windows (via Chocolatey or Scoop)
$ choco install jbang
# OR
$ scoop install jbang

# Universal curl installer (Linux / macOS)
$ curl -s https://jbang.dev/installer.sh | bash

Verify your installation by running:

$ jbang --version

2. Creating Your First JBang Script (Java 21)

You can generate a new JBang template script instantly using the CLI command:

$ jbang init --template=cli HelloJBang.java

This creates a self-contained executable Java script:

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 21

import static java.lang.System.out;

public class HelloJBang {

    public static void main(String... args) {
        String name = args.length > 0 ? args[0] : "World";
        out.println("Hello " + name + " from JBang & Java 21!");
    }
}

To execute the script directly, pass it to jbang (or make the file executable on Unix systems):

$ jbang HelloJBang.java Developer

# Output:
# Hello Developer from JBang & Java 21!

3. Adding Maven Dependencies (`//DEPS`)

One of JBang's most powerful features is inline dependency resolution. Instead of creating a Maven pom.xml or Gradle build file, declare third-party libraries using //DEPS directives:

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 21
//DEPS org.jsoup:jsoup:1.18.3
//DEPS com.fasterxml.jackson.core:jackson-databind:2.18.2

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class WebScraper {

    public static void main(String... args) throws Exception {
        out.println("Fetching website metadata...");
        Document doc = Jsoup.connect("https://www.mastertheboss.com").get();
        System.out.println("Page Title: " + doc.title());
    }
}

When you run jbang WebScraper.java, JBang automatically fetches the required JAR files from Maven Central, caches them locally, and sets up the classpath transparently.

4. Advanced JBang Features

A. Building Native Executables with GraalVM (`//NATIVE`)

Need instant CLI startup time with zero JVM cold boot overhead? Add the //NATIVE directive or pass the --native flag to build a standalone binary using GraalVM:

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 21
//NATIVE

public class FastCli {
    public static void main(String... args) {
        System.out.println("Native execution speed!");
    }
}
# Compile into a native OS binary
$ jbang build --native FastCli.java

# Run the compiled binary
$ ./FastCli

B. Splitting Code into Multiple Source Files (`//SOURCES`)

For larger automation scripts, you can modularize your code across multiple Java files using //SOURCES:

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 21
//SOURCES HelperService.java

public class MainApp {
    public static void main(String... args) {
        HelperService.sayHello();
    }
}

5. JBang vs. JShell vs. Traditional Java

Feature Traditional Java JShell (REPL) JBang
Project Setup Requires Maven/Gradle & Directory Structure Interactive shell only Zero setup (Single File)
Dependency Injection Configured in pom.xml / build.gradle Manual Classpath flags Inline via //DEPS
JDK Version Locking Global system environment System JDK Per-script via //JAVA 21
Native Compilation Complex GraalVM Native-Image Maven Plugin Not supported Built-in via //NATIVE

Frequently Asked Questions (FAQs)

Q1: Does JBang recompile scripts on every execution?

No. JBang intelligently checks script checksums and caches compiled bytecode. Subsequent script runs execute instantly without recompilation unless the underlying source code or dependencies change.

Q2: Can I run JBang scripts in CI/CD pipelines (GitHub Actions, GitLab CI)?

Yes. JBang provides an official GitHub Action (jbangdev/jbang-action) making it simple to execute Java automation scripts inside your CI/CD workflows.

Q3: How do I open and edit a JBang script in IntelliJ IDEA or VS Code?

Run jbang edit --open=idea Script.java. JBang generates a temporary IDE project environment with full auto-completion and classpath resolution automatically.

Conclusion

JBang brings the agility of scripting languages to Java without sacrificing the power of the JVM ecosystem. Whether you are building automation tooling, devops scripts, or prototyping REST APIs, JBang is an essential tool for modern Java developers.