Jbang - The Power of Shell Scripting for Java

DZone 's Guide to

Jbang - The Power of Shell Scripting for Java

Shell scripting for Java

· Java Zone ·
Free Resource

Whenever a new Java library is out, developers would be curious to try it out. However, it is sometimes cumbersome to set up the project in the IDE or as a Maven project along with the dependencies. Of course, classpaths can be set in the command line, but it is still cumbersome for the lazy developers ;-)

jbang is here to the rescue!

Setting up the project to try out a library or running a Java file is going to be a thing of the past with jbang!

Of course, Java has the JShell, but jbang seems to be better in many ways.

jbang is a neat little tool written by Max Rydahl Andersen (maxandersen) that

  • enables running java as scripts anywhere with java or jshell
  • helping to try out any Java API without the need to install, setup nor configure the project for Maven, Gradle or any other build system

A quick feature list for jbang is:

  • Requires Java 8 or above, but Java 11 is recommended
  • Dependency declarations through //DEPS group:artifact:version or @Grab annotations
  • Compile and runtime options with //JAVAC_OPTIONS <flags> and //JAVA_OPTIONS <flags>
  •  Supports the platforms - Windows, Linux and Mac
  • Transparently launch JavaFX application on Java 8 and above
  • And much more...

Now, let's quickly see the power of jbang! Let's try out a nice library — Jsoup — that is a Java library that provides very convenient APIs for fetching URLs, extracting and manipulating HTML content using DOM methods and CSS selectors, or in short JQuery like syntax to work with the DOM.

First, we will have to init the jbang class using

jbang init hellojsoup.java

This would create a hellojsoup.java file with the hellojsoup class with the main method. At the top of the file it also has commands for the Unix !shebang and the DEPS declaration.

The DEPS declaration is a comment where we specify the Gradle-like dependencies of the format — groupId:artifactId:version

In our case it will be org.jsoup:jsoup:1.13.1

Here is the code that does a simple call to Wikipedia and displays the headlines in the news section. It is a slightly modified version as given in the Jsoup site.


To run the code, you will have to type the following command, where the run is an optional command.

jbang run hellojsoup.java

Running this program would display the day's headlines in the news section. As of this writing, the following was the output.


The amazing part of jbang is that it is so simple and easy. I believe it is not just to try out libraries but will grow to be a tool that will have a bigger production usage in the future as well, especially in Microservices and Serverless applications. No complex server setup, just one single Java file doing the job just fine.

It has many examples that includes vertx, undertow, JavaFX, resteasy, ratpack and much more. It also has support for all the major IDEs.

For more details and documentation, head on to https://github.com/jbangdev/jbang

jbang looks more promising and it would be no wonder if it gets included in the JDK in the future. Give it a try!

java, shell script

Published at DZone with permission of Prabhu Rengaswamy . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}