Intro
Scripting in java, that sounds pretty strange, right? I have to admit it too until I start working with JBang for minimal command line tools.
The journey is not straight forward as bash script (#! /bin/bash script) where assumption specific libraries, tools, other user’s scripts and environment variables are available to consume. The trade off for that hardness is an opening gate to vast ecosystem of java libraries which are widely used in my projects. That’s amazing !!! For me, as a long time Java user, I think I now can be extremely productive in making scripts, reusing them my projects. Here are a few notes for using JBang.
The good parts
-
JBang is a like a build tool when it can build, package and export fat jar files (JBang calls it as portable). This is quite a hack, we can build a collection of jar files then get it distributed into target running machine where java runtime is available then we can run our scripts. Because, Java runtime is available for many architectures hence it can be considered as “run ever-where”. Does it sound familiar to you: write once, run everywhere?
-
JBang works best with IDE (productivity here where all heavy lifting works like auto-complete, type inference (Java 11+), debug script. Anyway, if I work with code, I always prefer using an IDE rather than a simple text editor. JBang manages scripts as “apps” at OS level hence you can call the installed apps on OS shell freely. Let’s say we install JBang script name breaking and have it installed as app by JBang. Then we can open any terminal and start using it by entering command breaking —name Jessy ! Pretty neat, right?
-
JBang manages dependencies for your scripts via its own markup
//DEPS groupid.artifact.version
. I think this trick is amazing. Now, we can bring the Java jungle into your script effortlessly. Who cares for it if we don’t actually have to manage dependencies manually when packaging? JBang does all the heavy jobs regarding to library dependencies management. Again comparing to python, where I see we often have a requirements.txt file for a particular script and have pip command does the rest for us. It is okay for a few dependencies but when I have a long list of //DEPS, I really want all of them get organized into a separate file. Just like Gradle or Maven tool. -
JBang also can manage dependencies on Java class files from other directory from current directory by markup
//FILES path-to-java-class-file
Yeah, pretty much the same from import statement from python but the manage path manually is quite hassle for me especially when I move stuffs around under sub directories. Once I settle down project structure, I think I can live with it. -
JBang can create/manage script templates. Yeah, you can create and reuse your templates from time to time. Java is verbose, I totally understand that and I am also suffered from that weakness in certain scenarios. But hey man, if IDE can help me overcoming that with a few key strikes then why do we have to bother with that?
The not so good parts
Alright, this is not a silver bullet to solve all problems without any hiccup. Here are few things I’ve learnt along with my experiment with JBang.
You still really need IDE so as to be productive.
User experience to build a script is acceptable but building a scripting ecosystem around JBang is quite an open journey. I’ve learnt it the hard way where I restructured my project many times. For an example, this part is for common classes and that part need to be a separate directory. This mean if you really want to invest in JBang scripting, you may build and break stuffs a few times before you get the click! on your project.
Some of Java API is quite alienated ones since I rarely use them. You will fell the verbose side of java on this. Some times, to achieve a simple task, you need to write a few line codes while comparing to python or bash, it is only one line. The struggle of Dependency Injection (DI) container for certain libraries. Simply do I miss the magic of DI here. Most of projects I have been through, DI container just saves days of effort if we manually hand crafted code. I wish JBang someday support DI out of the box. That would be a game changer for java scripting for sure.
To make CLI tools, I have to learn picocli library. That’s a burden on learning curl. For an example, to handle options and parameters, I get it straight with getops from bash script but now, all the stuff become Java class fields with annotation on them.
Interact with OS native tools is not straight forward as in bash script.
I will come back JBang again with hand-on public repository in near future but in the mean times, if you are Java user and want to a sip of it for scripting task, have it a shot and I bet you feel pretty comfortable and productive in a few hours. Jumping to below home page and cli reference will kick-start you quite a lot.
P/S: install IntelliJ Jbang plugin at the beginning will save you a lot of time.
References
- JBang documentation https://www.jbang.dev/documentation/guide/latest/index.html
- cli reference https://www.jbang.dev/documentation/guide/latest/cli/jbang.html