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.
I made a thing.— Max Rydahl Andersen (@maxandersen) January 2, 2020
J'bang - use java for scrpting.https://t.co/9vAHt4NfRm
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 (example.com) 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. test.example.com
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
CloudflareDDNSUpdate.java class to write the logic.
|I have used beta.ipinfo.in 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 -
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.
//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.
jbang CloudflareDDNSUpdate.java ./cf.properties 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
CloudflareDDNSUpdate.java 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!
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 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.