JBang – Scripting with Java Like Never Before
For decades, Java has been known as a powerful but verbose and heavyweight language—great for enterprise applications, not so much for scripts and quick tools. Enter JBang: a modern tool that breathes new life into Java by making it scriptable, lightweight, and even fun!
๐ What is JBang?
JBang is a tool that allows you to run Java files as scripts — directly from the terminal — without needing to create a Maven or Gradle project. It's like using Python or Bash, but with Java!
JBang compiles and runs your Java files behind the scenes, automatically handling:
- Dependency management
- Project setup
- JDK selection
- Script execution
With JBang, you can write a complete Java program in a single file, include libraries with simple annotations, and run it instantly.
⚙️ Installing JBang
JBang supports all major OS platforms and can be installed in multiple ways:
- macOS:
brew install jbangdev/tap/jbang - Linux:
curl -Ls https://sh.jbang.dev | bash - Windows: Download from jbang.dev or use Scoop/Chocolatey
- SDKMAN:
sdk install jbang
Once installed, verify with:
jbang version
๐ง Writing Your First Java Script
Create a file called Hello.java with the following:
//usr/bin/env jbang $0 $@ ; exit $?
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, JBang!");
}
}
Run it:
jbang Hello.java
No project setup, no compilation step — just pure Java joy!
๐ฆ Adding Dependencies
JBang allows you to add dependencies inline using //DEPS:
//DEPS com.squareup.okhttp3:okhttp:4.9.3
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class FetchUrl {
public static void main(String[] args) throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.github.com")
.build();
var response = client.newCall(request).execute();
System.out.println(response.body().string());
}
}
This is incredibly useful for building quick tools, integrations, or testing libraries.
๐️ Advanced Features
- Top-level scripts: Java code without class wrappers (since Java 11+)
- Template system: Reuse boilerplate with
jbang init --template - Alias support: Create your own local CLI tools with
jbang alias add - Native packaging: Convert your script to a native binary with
jbang export native
๐งช Comparing JBang with Other Tools
| Feature | JBang | JShell | Maven |
|---|---|---|---|
| Quick scripting | ✅ | ✅ | ❌ |
| Dependency support | ✅ | Limited | ✅ |
| Classless top-level code | ✅ | ✅ | ❌ |
| Reusable scripts | ✅ | ❌ | ✅ |
๐ก Use Cases
- ๐ผ Automating repetitive developer tasks
- ๐ฌ Prototyping APIs and data transformations
- ๐ฆ Creating developer CLIs and tools
- ๐ Quick data visualizations and reports
- ๐ Teaching Java in workshops without setup overhead
๐ Resources & Links
๐ฏ Conclusion
JBang is the future of Java scripting. It strips away the verbosity and lets you use Java like a modern scripting language. Whether you're building automation tools, quick utilities, or full-on CLI apps, JBang offers a frictionless developer experience.
✨ Give it a try, and Java might just become your new favorite scripting language.
Comments
Post a Comment