Maciej Walkowiak

Single file Java applications with JBang

Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped and tortured. Ukraine needs your help!

Help Ukraine Now!

Groovy has one feature I always wanted to have in Java - Grapes. Grapes let you define Maven dependencies inside the source code file and Groovy resolves them when you run the script. This enables writing applications that leverage whole ecosystem of Java libraries in a single-file.

import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.HttpGet

def httpClient = new DefaultHttpClient()
def url = ''
def httpGet = new HttpGet(url)

def httpResponse = httpClient.execute(httpGet)

new File('result.html').text =httpResponse.entity.content.text

While such thing cannot be done with pure Java, it can work with a help of JBang.


JBang - a CLI created by Max Rydahl Andersen (who happens to be also Quarkus co-lead) lets you run Java code with minimum setup. It handles resolving dependencies and even installing the JDK version you declare.

This is an equivalent of the Groovy script from above:

//usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17
//DEPS org.apache.httpcomponents:httpclient:4.2.1


import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpClientDemo {
    public static void main(String[] args) throws IOException {
        var httpClient = new DefaultHttpClient();
        var httpGet = new HttpGet("");

        var httpResponse = httpClient.execute(httpGet);

        try(var writer = new FileOutputStream("result.html")) {

Which can be run with:

$ jbang run

No Maven, no Gradle, no complex directory structure. Just one single command to use.

It makes Java feel almost like a scripting language, just better.

JBang with Spring Boot

Why not use this approach to develop a Spring Boot application? Lets try:

//usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17
//DEPS org.springframework.boot:spring-boot-starter-web:2.7.5

package app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class MyApp {
    public static void main(String[] args) {, args);

    String hello() {
        return "hello world";


$ jbang run

starts a Spring Boot application on port 8080.

Building a JAR

JBang can also build an executable JAR file:

$ jbang export portable

It will create a MyApp.jar file containing our source code, and all the dependencies will land in lib directory. At this stage, there is no option to build a single fat-jar containing both our code and the dependencies.

Building a Dockerimage for JBang application

To make it a bit more production ready, we can easily build a Docker image:

FROM jbangdev/jbang-action AS build

RUN mkdir /app
COPY . /app

RUN jbang export portable

FROM openjdk:17-alpine
RUN mkdir /app/
RUN mkdir /app/lib
COPY --from=build /app/MyApp.jar /app/MyApp.jar
COPY --from=build /app/lib/* /app/lib/
CMD "java" "-jar" "MyApp.jar"

(please don't take it as an example of production ready Dockerfile - it's just for educational purposes 🙃)

Then build & run:

docker build . -t my-app
docker run -p 8080:8080 my-app

IDE integration

I can't really write Java code without a proper IDE and autocompletion. The good thing is that there are JBang plugins for major IDEs:


I think JBang is a great addition to Java ecosystem. It is not a replacement for regular toolchain, but definitely enables experimenting with Java language and Java libraries by taking away all the heavy lifting related with setting up build files.