A logo showing the text blog.marcnuri.com
Español
Home»Backend Development»What is JBang? The Complete Guide to Java Scripting

Recent Posts

  • 2025 Year in Review: The Year of AI
  • Synology DS224+: How to upgrade hard drives in RAID 1
  • Fabric8 Kubernetes Client 7.5 is now available!
  • Boosting My Developer Productivity with AI in 2025
  • Black Box vs White Box Testing: When to Use Each Approach

Categories

  • Artificial Intelligence
  • Backend Development
  • Cloud Native
  • Engineering Insights
  • Frontend Development
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Quality Engineering
  • Tools

Archives

  • January 2026
  • December 2025
  • October 2025
  • September 2025
  • July 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • October 2017
  • August 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • November 2014
  • October 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

What is JBang? The Complete Guide to Java Scripting

2020-06-13 in Backend Development tagged Java / JBang / Quarkus by Marc Nuri | Last updated: 2026-01-24
Versión en Español

Introduction

Java is a powerful language, but traditionally it hasn't been known for rapid scripting or prototyping. The verbosity and boilerplate code have often been cited as reasons why developers turn to Python, JavaScript, Bash, or other languages for scripting tasks.

JBang is a tool that changes this perception by making Java a first-class citizen in the scripting world. Created by Max Andersen, JBang addresses a simple question: why should writing a quick Java utility require more ceremony than Python or Bash?

A picture of the JBang logo
A picture of the JBang logo

In this comprehensive guide, you'll learn what JBang is, how it compares to alternatives, and how to leverage its full potential for your development workflow.

What is JBang?

JBang is a command-line tool that transforms Java into a scripting language, making it as accessible as Python or Shell scripting. It allows developers to create, edit, and run self-contained Java programs with minimal setup. There's no need for project configuration or build files.

By eliminating the need for traditional build tools like Maven or Gradle for simple scripts, JBang simplifies Java development, enabling experimentation and quick prototyping. Whether you're writing a quick one-off script or prototyping an idea, JBang allows you to work in plain Java syntax while automatically managing dependencies and even JDK versions.

With JBang, you can:

  • Run Java code directly from .java files, URLs, or even Maven coordinates
  • Manage dependencies inline using simple comment directives (//DEPS) or annotations
  • Support multiple languages including Java, Kotlin, Groovy, JShell, and Markdown
  • Create native executables using GraalVM native-image
  • Debug and edit scripts seamlessly in popular IDEs like IntelliJ IDEA, VS Code, and Eclipse
  • Share scripts via the JBang App Store and catalogs

Its tagline, "Unleash the power of Java," perfectly encapsulates its mission to make Java more approachable for scripting and lightweight tasks.

JBang vs JShell vs Java Single-File

Java offers several options for running code without a full project setup. Understanding the differences helps you choose the right tool for your needs.

JShell (Java 9+)

JShell is Java's built-in REPL (Read-Eval-Print Loop), introduced in Java 9. While useful for quick experimentation, it has significant limitations for scripting:

  • Poor error handling: JShell ignores syntax errors and proceeds with execution, returning exit code 0 regardless of failures
  • Difficult argument passing: Passing command-line arguments to JShell scripts requires workarounds
  • No dependency management: You cannot easily include external libraries without manual classpath configuration

Java Single-File Source-Code Programs (JEP 330, Java 11+)

Java 11 introduced JEP 330, allowing you to run single .java files directly without explicit compilation:

bash
java HelloWorld.java

This approach is more robust than JShell. Syntax errors and exceptions properly return non-zero exit codes. However, it still lacks dependency management; you must distribute any required JARs separately.

JBang: The Complete Solution

JBang combines the best of both approaches while adding powerful features neither can offer:

FeatureJShellJava Single-File (JEP 330)JBang
Requires installationNo (built-in)No (built-in)Yes
Dependency managementNoNoYes (//DEPS)
Error handlingPoor (always exits 0)GoodGood
Command-line argumentsDifficultEasyEasy
Auto-download JVMNoNoYes
IDE supportBasicBasicFull
Multiple languagesNoNoYes (Java, Kotlin, Groovy)
Native image supportNoNoYes (GraalVM)
Script sharingNoNoYes (App Store, catalogs)

If you need dependency management, JBang is the way to go.

Why Use JBang?

For years, Java developers have lacked a simple scripting solution. JBang fills this gap with features such as:

  • Ease of Use: Write a standalone .java file with a main method and run it immediately
  • Automatic Dependency Management: Resolve dependencies from Maven Central or other repositories with simple comments (e.g., //DEPS ...)
  • Cross-Platform: Works on Windows, macOS, Linux, and AIX. True "Write once, run anywhere"
  • JDK Bootstrapping: No Java installed? JBang can download and install the required JDK for you
  • Modern Java Support: Use preview features, latest Java versions, and compile-time options
  • Framework Integration: Compatible with Quarkus, Spring Boot, Micronaut, and Apache Camel
  • CI/CD Ready: Run JBang scripts in GitHub Actions, GitLab CI, or any CI/CD pipeline
  • Native Image Compilation: Create standalone native executables with GraalVM

Getting Started with JBang

Let's get JBang installed and running on your system.

Installation

JBang offers multiple installation methods for different platforms:

Linux/macOS/AIX/WSL (bash):

bash
curl -Ls https://sh.jbang.dev | bash -s - app setup

Windows PowerShell:

Windows PowerShell
iex "& { $(iwr -useb https://ps.jbang.dev) } app setup"

Package Managers:

SDKMan
sdk install jbang
Homebrew (macOS)
brew install jbangdev/tap/jbang
Chocolatey (Windows)
choco install jbang
Scoop (Windows)
scoop install jbang

Important

Consider verifying the script's source and checksum before execution for security best practices.

Alternatively, you can download the JBang binaries for your system directly from the website.

After installation, verify JBang is correctly installed:

bash
jbang --version

Writing Your First Script

Let's create a simple "Hello, World!" script using JBang.

1. Initialize the Script

JBang provides a simple way to create a new script:

bash
jbang init HelloWorld.java

This creates a file named HelloWorld.java with a basic template:

HelloWorld.java
///usr/bin/env jbang "$0" "$@" ; exit $?

import static java.lang.System.*;

public class HelloWorld {

    public static void main(String... args) {
        out.println("Hello World");
    }
}

2. Run the Script

Execute the script with:

bash
jbang HelloWorld.java

You should see Hello World in your terminal.

On Unix-like systems, you can also make the script executable and run it directly:

bash
chmod +x HelloWorld.java
./HelloWorld.java

The shebang line (///usr/bin/env jbang "$0" "$@" ; exit $?) tells the system to use JBang to run the script.

JBang Script Directives

JBang scripts use special comments (directives) to configure behavior. These must appear at the beginning of the file, before any code.

Dependency Management

The //DEPS directive adds Maven dependencies to your script:

WithDependencies.java
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.7.6
//DEPS com.google.code.gson:gson:2.11.0

import picocli.CommandLine;
import com.google.gson.Gson;

public class WithDependencies {
    public static void main(String... args) {
        System.out.println("Dependencies loaded successfully!");
    }
}

JBang automatically downloads dependencies from Maven Central and adds them to the classpath.

Custom Repositories

Use //REPOS to add custom Maven repositories:

//REPOS central,jitpack
//REPOS custom=https://repo.company.com/maven2
//DEPS com.github.user:repo:tag

Java Version

Specify the required Java version with //JAVA:

//JAVA 21        // Exact version
//JAVA 17+       // Minimum version (17 or higher)

JBang automatically downloads the required JDK version if not available on your system.

Preview Features

Enable preview features for experimenting with the latest Java capabilities:

//JAVA 23+
//PREVIEW

void main() {
    System.out.println("Using simplified main with preview features!");
}

Complete Directives Reference

Here's a comprehensive table of all available JBang directives:

DirectiveDescriptionExample
//DEPSAdd Maven dependencies//DEPS org.slf4j:slf4j-api:2.0.9
//REPOSSpecify Maven repositories//REPOS jitpack,central
//SOURCESInclude additional source files//SOURCES utils/*.java
//FILESInclude resource files//FILES config.properties
//JAVASpecify Java version//JAVA 21+
//PREVIEWEnable preview features//PREVIEW
//COMPILE_OPTIONSSet compiler options//COMPILE_OPTIONS -Xlint:all
//RUNTIME_OPTIONSSet JVM runtime options//RUNTIME_OPTIONS -Xmx2g
//NATIVE_OPTIONSGraalVM native-image options//NATIVE_OPTIONS --no-fallback
//MAINOverride default main class//MAIN com.example.App
//MANIFESTAdd JAR manifest entries//MANIFEST Built-By=Developer
//GAVMaven coordinates for script//GAV com.example:app:1.0.0
//DESCRIPTIONScript description//DESCRIPTION A utility script
//KOTLINSpecify Kotlin version//KOTLIN 2.0.21
//GROOVYSpecify Groovy version//GROOVY 4.0.24
//CDSEnable Class Data Sharing//CDS
//JAVAAGENTConfigure Java agent//JAVAAGENT myagent.jar

Multi-Language Support

JBang isn't limited to Java. It also supports multiple JVM languages.

Kotlin

Create and run Kotlin scripts:

bash
jbang init -t hello.kt HelloKotlin.kt
jbang HelloKotlin.kt

You can control the Kotlin version:

HelloKotlin.kt
//KOTLIN 2.0.21

fun main() {
    println("Hello from Kotlin!")
}

Groovy

Create Groovy scripts:

bash
jbang init -t hello.groovy HelloGroovy.groovy
jbang HelloGroovy.groovy

Specify the Groovy version:

HelloGroovy.groovy
//GROOVY 4.0.24

println "Hello from Groovy!"

Markdown

You can even "run" Markdown files! JBang extracts and executes Java or JShell code blocks:

bash
jbang init -t readme.md README.md
jbang README.md

This is great for executable documentation and tutorials.

GraalVM Native Images

JBang can compile your scripts to native executables using GraalVM's native-image tool. The resulting binaries launch in milliseconds, have a smaller memory footprint, and run independently without a JVM installation.

Creating a Native Image

bash
jbang --native HelloWorld.java

The first run compiles the native image (which takes longer). Subsequent runs use the cached native binary for instant startup.

Customizing Native Compilation

Use the //NATIVE_OPTIONS directive for fine-tuning:

NativeApp.java
///usr/bin/env jbang "$0" "$@" ; exit $?

//NATIVE_OPTIONS --no-fallback -O2
//NATIVE_OPTIONS -H:+ReportExceptionStackTraces

public class NativeApp {
    public static void main(String... args) {
        System.out.println("Running as native binary!");
    }
}

Prerequisites for Native Images

You need GraalVM with native-image installed:

bash
# Install via SDKMan
sdk install java 21.0.2-graalce
sdk use java 21.0.2-graalce

# Or download from graalvm.org and install native-image
gu install native-image

Native images are ideal for CLI tools, serverless functions, and container images with minimal size.

IDE Integration

JBang integrates seamlessly with popular IDEs for a full development experience.

VS Code

Install the JBang VS Code extension for syntax highlighting, dependency completion, and debugging support.

IntelliJ IDEA

The JBang IntelliJ plugin provides full IDE support, including code completion for dependencies and run configurations.

Opening Scripts in IDE

Use the edit command to open any script with full IDE support:

bash
jbang edit HelloWorld.java

JBang creates a temporary project with proper classpath configuration, giving you code completion, debugging, and refactoring capabilities.

JBang App Store and Catalogs

JBang includes a powerful system for sharing and discovering scripts.

The JBang App Store

The JBang App Store allows you to discover and run scripts published by the community:

bash
# Run cowsay
jbang cowsay@jbangdev "Hello JBang!"

# Run JReleaser
jbang jreleaser@jreleaser

# Search Maven Central
jbang gavsearch@jbangdev hibernate

Creating Aliases

Create local aliases for frequently used scripts:

bash
jbang alias add --name hello https://github.com/jbangdev/jbang-examples/blob/HEAD/examples/helloworld.java
jbang hello

Catalog Files

Share collections of scripts via jbang-catalog.json:

jbang-catalog.json
{
  "aliases": {
    "hello": {
      "script-ref": "HelloWorld.java",
      "description": "A simple hello world script"
    },
    "cli": {
      "script-ref": "MyCLI.java",
      "description": "Command-line utility"
    }
  }
}

Host this file in a Git repository, and others can use your scripts:

bash
jbang hello@user/repo

Installing as System Commands

Install JBang scripts as system-wide commands:

bash
jbang app install hello.java
hello  # Now available as a command

Framework Integration

JBang works seamlessly with popular Java frameworks.

Quarkus

Create a single-file Quarkus REST API:

QuarkusApp.java
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS io.quarkus.platform:quarkus-bom:3.17.7@pom
//DEPS io.quarkus:quarkus-rest

//JAVA 17+

//Q:CONFIG quarkus.http.port=8080

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/hello")
public class QuarkusApp {

    @GET
    public String hello() {
        return "Hello from Quarkus!";
    }
}

Run it:

bash
jbang QuarkusApp.java
# Visit http://localhost:8080/hello

Apache Camel

Install Camel JBang and run integration routes:

bash
# Install Camel CLI
jbang app install camel@apache/camel

# Run a route
camel run route.yaml

Example YAML route:

route.yaml
- from:
    uri: "timer:tick?period=1000"
    steps:
      - set-body:
          constant: "Hello Camel!"
      - log: "${body}"

Picocli CLI

Create professional command-line applications:

bash
jbang init -t cli MyCLI.java

This generates a script with Picocli for argument parsing, help generation, and shell completion.

CI/CD Integration

JBang is perfect for automation scripts in CI/CD pipelines.

GitHub Actions

Use the official JBang GitHub Action:

.github/workflows/jbang.yml
name: Run JBang Script
on: [push]

jobs:
  jbang:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Run JBang script
        uses: jbangdev/jbang-action@v0.135.1
        with:
          script: myscript.java
          scriptargs: "arg1 arg2"

Docker

Run JBang scripts in containers:

bash
docker run -v $(pwd):/scripts jbangdev/jbang-action /scripts/myscript.java

Exporting to Maven or Gradle

When your script grows into a full project, export it to a traditional build system:

Export to Gradle

bash
jbang export gradle --group com.example --artifact myapp hello.java

Export to Maven

bash
jbang export maven --group com.example --artifact myapp hello.java

JBang converts your //DEPS directives to proper pom.xml or build.gradle dependencies.

Other Export Formats

bash
# Create a fat JAR
jbang export fatjar hello.java

# Create a portable package
jbang export portable hello.java

# Create a JLink runtime image
jbang export jlink hello.java

# Export to Maven repository
jbang export mavenrepo hello.java

Real-World Use Cases

JBang excels in scenarios where traditional Java setup is overkill:

DevOps Automation

Replace complex shell scripts with type-safe Java:

deploy.java
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.7.6
//DEPS org.zeroturnaround:zt-exec:1.12

import picocli.CommandLine;
import picocli.CommandLine.Command;
import org.zeroturnaround.exec.ProcessExecutor;

@Command(name = "deploy", description = "Deploy blog")
public class deploy implements Runnable {

    @CommandLine.Option(names = {"-e", "--env"}, required = true)
    String environment;

    public void run() {
        System.out.println("Deploying blog.marcnuri.com to " + environment);
        // Your deployment logic here
    }

    public static void main(String... args) {
        new CommandLine(new deploy()).execute(args);
    }
}

Quick Prototyping

Test libraries without creating a project:

TestLibrary.java
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS com.squareup.okhttp3:okhttp-jvm:5.3.2

import okhttp3.*;

public class TestLibrary {
    public static void main(String... args) throws Exception {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
            .url("https://blog.marcnuri.com")
            .build();
        try (Response response = client.newCall(request).execute()) {
            System.out.println(response.body().string());
        }
    }
}

Educational Demos

Share executable examples via URLs:

bash
jbang https://github.com/marcnuri-demo/blog-tutorials/blob/main/Demo.java

Students can run examples without any setup.

Frequently Asked Questions

Do I need Java installed to use JBang?

No! JBang can automatically download and manage Java for you. If Java isn't installed, JBang downloads the required version on first run.

Can I use JBang with my existing Maven/Gradle project?

JBang is designed for standalone scripts, but you can:

  • Use jbang export maven|gradle to convert scripts to projects
  • Use the JBang Maven/Gradle plugins to run scripts from existing projects

What's the difference between JBang and JShell?

JBang offers dependency management, multiple language support, native image compilation, and proper error handling. JShell is built-in but limited to interactive exploration without external dependencies.

Can I use JBang in production?

Absolutely! JBang is used in production for CLI tools, automation scripts, and microservices. For production deployments, consider exporting to native images or traditional build systems.

How do I debug JBang scripts?

Use jbang edit to open your script in an IDE with full debugging support, or run with jbang --debug script.java and attach a remote debugger.

Can I use private Maven repositories?

Yes, use the //REPOS directive to add private repositories:

//REPOS private=https://repo.company.com/maven2

For authentication, configure your ~/.m2/settings.xml as you would for Maven.

Conclusion

JBang brings the convenience of scripting languages to Java without sacrificing any of its power. Whether you're automating DevOps tasks, testing a new library, prototyping an idea, or introducing Java to newcomers, JBang eliminates the setup overhead that has historically made Java feel heavyweight for quick tasks.

Key takeaways:

  • Zero setup: Run Java files directly without project configuration
  • Dependency management: Add libraries with simple //DEPS comments
  • Multi-language: Support for Java, Kotlin, Groovy, and more
  • Native images: Compile to standalone executables with GraalVM
  • IDE support: Full development experience in VS Code, IntelliJ, and Eclipse
  • CI/CD ready: GitHub Actions, Docker, and pipeline integration

Give JBang a try and see how it can streamline your Java development workflow!

Official Resources:

  • JBang Website
  • JBang Documentation
  • JBang GitHub Repository
  • JBang App Store
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
Building a GitHub Dependents Scraper with Quarkus and PicocliAccess the Kubernetes API from a Pod in Java
© 2007 - 2026 Marc Nuri