← Getting Started ← Home

What is JBang?

JBang lets you run a Java source file the same way you'd run a shell script — no pom.xml, no build.gradle, no compile step. Dependencies are declared in comments at the top of the file, and JBang fetches them, compiles the file and runs it. That's the entire loop.

Why every example uses it

Every project in this playground is a single .java file that starts with a JBang preamble. That means you can clone the repo, plug in a Sense HAT and run any example with a single command — no IDE, no Maven download, no target/ directory.

jbang projects/Dice/Dice.java

Anatomy of a JBang script

The preamble at the top of each example does three things: declares the Java version, lists Maven dependencies, and points JBang at the repositories those dependencies live in.

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25
//DEPS com.pi4j:pi4j-core:4.0.1
//DEPS com.pi4j:pi4j-plugin-ffm:4.0.1
//DEPS com.pi4j:pi4j-drivers-igfasouza:1.1.1-SNAPSHOT
//REPOS mavencentral,mavenlocal,sonatype-snapshots=https://s01.oss.sonatype.org/content/repositories/snapshots/

import com.pi4j.Pi4J;
import com.pi4j.drivers.hat.raspberry.SenseHat;

public class Hello {
    public static void main(String[] args) {
        var pi4j = Pi4J.newAutoContext();
        var hat  = new SenseHat(pi4j);
        hat.setPixel(0, 0, 0xFF0000);
    }
}

The ///usr/bin/env jbang shebang on the first line lets you mark the file executable and run it directly (./Hello.java) on Linux and macOS.

Installing JBang

On the Raspberry Pi (and pretty much everywhere else):

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

Or via a package manager — sdkman, brew, choco, apt — all listed on the JBang site. JBang installs its own JDK on first use if one isn't available, which keeps the on-device setup minimal.

Handy commands

  • jbang Foo.java — run a script.
  • jbang --edit Foo.java — open the script in an IDE with dependencies wired up.
  • jbang app install --name foo Foo.java — install the script as a system-wide command called foo.
  • jbang cache clear — nuke the local cache when a SNAPSHOT gets stuck.

Links