Using Java for scripting, with Jbang

I usually write bash script for any task automation. Sometime shell scripting is tricky if you are not very used to for shell commands. One day, while cruising through the never ending twitter wall, I came across this tweet. "use java for scripting" was good enough for me to try it out.

What is j’bang?

j’bang, created by Max Andersen, enables you to use java for scripting. It supports Java 8 and onwards. It supports dependency resolution, and IDE editing in addition to many other things. Max has introduced it very well on his blog here.

To install jbang, I used sdkman and ran - sdk install jbang. I tried the the hello world example and it worked for me. Next obvious thing was to find something more interesting example to try.

Cloudflare DDNS updates

I use Cloudflare for managing my domains. I have a shell script that does a Dynamic DNS update for my domain. This task requires accessing Cloudflare APIs and perform the update. This is a perfect example to create a java based script instead of a shell script.

For calling Cloudflare APIs, we need three configuration elements -

  • zoneId: Each zone ( on CloudFlare has a unique number identifier. You can find it on your cloudflare site dashboard.

  • fqdn: Target DNS name to update your public ip address. eg.

  • cfApiToken: A Cloudflare API Token created from Cloudflare dashboard. A token with Edit DNS permission and restricted to specific zone (your domain) worked well for this task.

Once I have these three values, I simply created a class to write the logic. source:

I have used built on Cloudflare workers to fetch public ip of current network. You can replace it with any other mechanism if you wish.

This class requires a path to cloudflare configuration properties file that contained these three properties - Cloudflare configuration properties
zoneId=<<your zone id>>
fqdn=<<your dns fqdn>>
cfApiToken=<<your api token>>

j’bang in action

With the java class and configuration file, we are ready to run it with j’bang. First three lines of the file has a special meaning for j’bang processing.

First line //usr/bin/env jbang "$0" "$@" ; exit $? allows this file to be run as a script on shell.

Second and third lines starting with //DEPS …​ declares the depenencies that j’bang then resolves.

Run jbang ./ for executing the file.


So, j’bang not only resolved our dependencies but also successfully executed our java file.

You can also run java file from the gist or github source. For running this file from gist, get the raw file url of gist and run below command.

But, How do we make it executable?

Change permissions of to make it executable and re-run without jbang command. In this case, the first line of the source helps to run this file as expected.


Woohoo! Java as an executable script and updated DNS!

What next?

This is definitely a good start and reason for continuing the "java as a scripting" exploration. As far as DDNS update is concerned, we could easily schedule that as a repeating task using cron jobs or other mechanism.

Have any thoughts, issues about j’bang? Don’t forget to let Max know or open an issue.

Have any thoughts about this article or want to share what you did with j’bang? Feel free to write in the comments below or share on twitter.

on twitter to get updates on new posts.

Stay updated!

On this blog, I post articles about different technologies like Java, MuleSoft, and much more.

You can get updates for new Posts in your email by subscribing to JavaStreets feed here -

Lives on Java Planet, Walks on Java Streets, Read/Writes in Java, JCP member, Jakarta EE enthusiast, MuleSoft Integration Architect, MuleSoft Community Ambassador, Open Source Contributor and Supporter, also writes at Unit Testers, A Family man!