JBang: scripting in Java

Intro
JBang makes running Java code as simple as running a Python script.With just one installation and a single command, you can write, edit, and run Java programs directly from your terminal — no need to set up full projects or complex build tools. This makes it a great option even for beginners.
Although Java 11 introduced support for running single-file Java programs, JBang takes it to the next level. It lets you create multi-file scripts, add libraries easily using Maven-style dependencies, and works smoothly with Java versions starting from 8 up to the latest like Java 17 and beyond.
From testing out quick ideas to building small tools or learning Java more efficiently, JBang offers a simple and effective way to get started. It's lightweight, beginner-friendly, and works across all major platforms — Windows, macOS, and Linux — making it a handy companion for any Java developer.
Making Sense of Modern Java
For beginners, starting with Java can sometimes feel like facing a Gordian Knot — not because the language is hard to learn, but because the ecosystem is vast and layered. In today’s fast-paced world, with AI tools and rapid prototyping becoming the norm, many developers are drawn to languages like Python for their simplicity and speed.

The Gordian knot story highlights how unexpected solutions can solve complex problems, similar to how Java's complexities can be addressed with the right tools. This metaphor emphasizes the need for innovation in coding.
Many learners struggle with Java's initial setup, such as choosing the right JDK and IDE, which can be daunting. This complexity often deters newcomers from fully engaging with the language.



The introduction of tools like JBang aims to simplify Java usage, making it more accessible. Such tools are essential for breaking down barriers that hinder new developers from entering the Java ecosystem.

Installation of JBang
Universal Bash Installer
We can install JBang on any major OS using just one Bash command. It automatically detects your platform and sets everything up for you.
curl -Ls <https://sh.jbang.dev> | bash -s - app setup
Installing JBang on Windows using PowerShell(Administrator)
For those users who have not installed bash on their Windows machine you can use PowerShell instead:
iex "& { $(iwr <https://ps.jbang.dev>) } app setup"
In both cases above, when you are downloading, you get something like that:

Check the version of JBang to be sure it got installed in your system:
$ jbang version
0.126.3
Note: If no JDK is found during installation, download the latest Java LTS version.
Our first Hello World using JBang
One-line statement to print Hello World in the shell
$ jbang -c 'println("Hello World");'
Hello World
Print Hello JBang! using command line arguments through the shell.
$ jbang -c 'println("Hello "+args[0])' JBang!
Hello JBang!
- c
evaluates code in JShell- No need to create a Java file
- Handles user arguments
- No need for public static or class main.
How to run JShell and perform basic calculations?
Run JShell with a specific version in interactive mode
jbang --java 21 -i

Print Hello World in JShell
jshell> print("Hello World")
Hello World
Create variables and use these variables in JShell

Create and run java files using JBang
Creating a hello.java file and running it without setting up build tools or even compilation.
Create a file in JShell
jbang init hello.java

Run the file in JShell
./hello.java
Notes:
When you run jbang init hello.java
, JBang generates a minimal runnable Java file pre-configured to print "Hello World"
. The template includes a main
method and uses the JBang shebang (//usr/bin/env jbang
) to enable direct execution. This serves as a quick starting point for scripting with Java.
The shebang line tells the system to execute the .java
file using jbang
, making it behave like a script without compiling it manually.
///usr/bin/env jbang "$0" "$@" ; exit $?
import static java.lang.System.*;
public class hello{
public static void main(String... args) {
out.println("Hello World");
}
}

Working with Multiple Java Files in JBang
JBang also supports running multiple Java files together. You can place all related .java
files in the same directory and run the main file directly using JBang. This allows you to organize your code better when working with helper classes or multiple components.
- Multiple Files in the Same Directory
///usr/bin/env jbang "$0" "$@" ; exit $?
public class Main {
public static void main(String[] args) {
System.out.println("Result: " + Helper.square(5));
}
}
public class Helper {
public static int square(int x) {
return x * x;
}
}

- Using
//SOURCES
directive (if helper is elsewhere)
///usr/bin/env jbang "$0" "$@" ; exit $?
///SOURCES utils/MathUtils.java
import utils.MathUtils;
public class Main {
public static void main(String[] args) {
System.out.println("10 * 10 = " + MathUtils.multiply(10, 10));
}
}
package utils;
public class MathUtils {
public static int multiply(int a, int b) {
return a * b;
}
}

- Using packages (like a full project)
///usr/bin/env jbang "$0" "$@" ; exit $?
///SOURCES com/telusko/utils/MathUtils.java
package com.telusko;
import com.telusko.utils.MathUtils;
public class Main {
public static void main(String[] args) {
System.out.println("10 * 10 = " + MathUtils.multiply(10, 10));
}
}
package com.telusko.utils;
public class MathUtils {
public static int multiply(int a, int b) {
return a * b;
}
}

Notes:
//SOURCES
→ for importing other Java files//FILES
→ for including files without compilation (useful for resources)- You can use
//DEPS
to include external libraries too.
Scripting for Java
JBang turns Java into a scripting language — just like Python, Bash, or Node.js. It allows you to run Java programs directly from .java files without manually compiling, creating projects, or setting up a build tool like Maven or Gradle.
Example: Prime Number Checker using JBang
This script checks whether a given number is prime. It's a pure console-based Java script that runs directly via jbang
.
$ jbang init PrimeCheck.java
///usr/bin/env jbang "$0" "$@" ; exit $?
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check if it's prime: ");
int number = sc.nextInt();
if (number <= 1) {
System.out.println(number + " is not a prime number.");
return;
}
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println(number + " is a prime number.");
else
System.out.println(number + " is not a prime number.");
}
}
How to Run
jbang PrimeCheck.java
hacki@MSI MINGW64 ~/PrimeScript
$ ./PrimeCheck.java
[jbang] Building jar for PrimeCheck.java...
Enter a number to check if it's prime: 12
12 is not a prime number.
Notes:
- On Windows Git Bash, the line starting with
//usr/bin/env jbang ...
is treated purely as a Java comment and is ignored by the shell — but using///
helps bypass parsing issues and prevents shell confusion, allowing the script to run. - On Linux/macOS, the same line
//usr/bin/env jbang "$0" "$@" ; exit $?
acts as a shell-compatible shebang workaround, so using//
works correctly for script execution.
Therefore, prefer using ///
on Windows (Git Bash), and stick with //
on Linux/macOS systems.
References
https://www.jbang.dev/documentation/jbang-all/latest/index.html
Comments ()