JBang cheatsheet (2022)

Here is my JBang cheatsheet which can help as a reference to code self-contained source-only Java programs with unmatched ease.

Getting started with JBang

Install JBang on Linux

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

Create a JBang class  from a template

jbang init --template=hello helloworld.java

List of available JBang templates

jbang template list

Run a Java class

jbang helloworld.java

Update JBang

jbang version --update

How to run a Java class in a JAR file

jbang jgroups-4.2.15.Final.jar --main=org.jgroups.tests.McastReceiverTest 

How to run a Java class using Maven GAV coordinates

jbang org.jgroups:jgroups:5.1.9.Final --main=org.jgroups.tests.McastReceiverTest 

How to run a Java code snippet:

echo "hello world" | jbang -c 'lines().forEach(s->println(s.substring(6)))`


Adding dependency  

//DEPS log4j:log4j:1.2.17

Adding Managed Dependencies (BOM) 

//DEPS io.quarkus:quarkus-bom:1.11.0.Final@pom
//DEPS io.quarkus:quarkus-resteasy
//DEPS io.quarkus:quarkus-smallrye-openapi

Using System properties in the comments

//DEPS org.openjfx:javafx-graphics:11.0.2:${os.detected.jfxname}

Using Environment variables in the comments

//DEPS org.openjfx:javafx-graphics:11.0.2:${env.jfxname}

Repository for dependencies (maven repository + custom repository)

//REPOS mavencentral,acme=https://maven.acme.local/maven

Adding file resources

//FILES resource.properties
//FILES META-INF/resources/index.html=index.html

JDK configuration

Setting JDK in your code

//JAVA 11 will force use of Java 11.
//JAVA 13+ will require at least java 13. Java 13 or higher will be used.

Install a JDK

jbang jdk install 17

Uninstall a JDK

jbang jdk uninstall 14

List available JDK

jbang jdk list

Setting default JDK

jbang jdk default 12

Debugging and JDK Settings

Debugging a Java class

jbang --debug helloworld.java

Setting java and javac options

//JAVAC_OPTIONS -source 11
//JAVA_OPTIONS -Xms128m -Xmx512m


Install an app from the Catalog

jbang app install CamelJBang@apache/camel

List of available apps

jbang app list