loading...
Cover image for jbang - The power of shell scripting for Java

jbang - The power of shell scripting for Java

rprabhu profile image Prabhu R Originally published at blog.teamnexus.in ・3 min read

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

//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.select.Elements;
import org.jsoup.nodes.Element;

public class hellojsoup {

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

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

Wikipedia, the free encyclopedia
2020 Twitter bitcoin scam
        https://en.wikipedia.org/wiki/2020_Twitter_bitcoin_scam
2020 Armenian?Azerbaijani skirmishes
        https://en.wikipedia.org/wiki/2020_Armenian%E2%80%93Azerbaijani_skirmishes
C/2020 F3 (NEOWISE)
        https://en.wikipedia.org/wiki/C/2020_F3_(NEOWISE)
Portal:Current events
        https://en.wikipedia.org/wiki/Portal:Current_events
Deaths in 2020
        https://en.wikipedia.org/wiki/Deaths_in_2020
Wikipedia:In the news/Candidates
        https://en.wikipedia.org/wiki/Wikipedia:In_the_news/Candidates

To try this code for yourself here is the repl.it

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 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!

Image Credits: Pixabay

Posted on by:

Discussion

markdown guide