DEV Community

Cover image for jbang - The power of shell scripting for Java
Prabhu R
Prabhu R

Posted on • Originally published at

jbang - The power of shell scripting for Java

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

  • allows running java as scripts anywhere
  • enables running java as scripts anywhere with java or jshell
  • helping trying 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 of 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

This would create a 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.

//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.jsoup:jsoup:1.13.1

import static java.lang.System.*;
import org.jsoup.nodes.Document;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

public class hellojsoup {

    public static void main(String... args) throws Exception {
        Document doc = Jsoup.connect("").get();
        Elements newsHeadlines ="#mp-itn b a");
        for (Element headline : newsHeadlines) {
          out.println(String.format("%s\n\t%s", headline.attr("title"), headline.absUrl("href")));
Enter fullscreen mode Exit fullscreen mode

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

jbang run

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

Wikipedia, the free encyclopedia
2020 Twitter bitcoin scam
2020 Armenian?Azerbaijani skirmishes
C/2020 F3 (NEOWISE)
Portal:Current events
Deaths in 2020
Wikipedia:In the news/Candidates
Enter fullscreen mode Exit fullscreen mode

To try this code for yourself here is the

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, esp. 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

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

Image Credits: Pixabay

Top comments (0)