Here is my JBang cheatsheet which can help as a reference to code self-contained source-only Java programs with unmatched ease. If you are new to JBang we recommend checking this article: JBang: Create Java scripts like a pro
Installation / Update
Install JBang on Linux
curl -Ls https://sh.jbang.dev | bash -s - app setup
How to upgrade JBang
jbang version --update
Run JBang Scripts
How to execute a JBang Script
jbang example.java
Run a Java class in a JAR file with JBang
jbang jgroups-4.2.15.Final.jar --main=org.jgroups.tests.McastReceiverTest
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)))`
Create an executable JAR from a JBang script:
jbang export portable MyApp.java
Template Management
Create a JBang class from a template
jbang init --template=hello helloworld.java
List of available JBang templates
jbang template list
JBang Configuration
How to add Adding a dependency to your script:
//DEPS log4j:log4j:1.2.17
Adding a GitHub project as dependency (JBang will download the project, build it as use it as a dependency)
//DEPS https://github.com/DiUS/java-faker
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
How to use Sources and Jars as external dependencies in isolation, without being built inside the project (requires jBang v0.109.0 or newer):
//DEPS myapp.jar //DEPS myclass.java
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}
How to pass the dependencies as command line argument to a Script:
jbang --deps org.matheclipse:matheclipse-core:2.0.0,org.matheclipse:matheclipse-io:2.0.0 test.java
Adding file resources
//FILES resource.properties //FILES META-INF/resources/index.html=index.html
Adding a Manifest file:
//MANIFEST version=${version:unknown}
Setting Quarkus properties
//Q:CONFIG quarkus.datasource.db-kind=postgresql
Remote file expansion (JBang will download the remote file – tagged with % – and pass it as argument)
jbang CountWords.java %https://github.com/dwyl/english-words/raw/master/words.txt
Repository Configuration
Configures a well-known repository (mavencentral) and a custom Repository “acme”
//REPOS mavencentral,acme=https://maven.acme.local/maven
Java Sources
How to specify the location of Java Sources (they will be built inside the project)
//SOURCES nested/*.java //SOURCES othernested/*.java
You can also pass the sources (and resources) location from the command line:
jbang --sources src/main/java --files src/main/resources script.java
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.
How to install a JDK
jbang jdk install 17
How to install a JDK from a location on your machine:
jbang jdk install 17 `sdk home java 17.0.4.1-tem`
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
Catalog
Install an app from the Catalog
jbang app install CamelJBang@apache/camel
List of available apps
jbang app list
Conclusion
With this JBang cheatsheet, you now have access to a concise and well-structured guide that covers the essential commands, syntax, and functionalities of jbang. Whether you are a developer, a student, or simply curious about this powerful Java-based scripting tool, this cheatsheet will empower you to streamline your scripting workflow, automate tasks, and boost productivity.