Taking Java from small to big with JBang

theanonymousone1 pts0 comments

Sombriks HQ - Big things have small beginnings: taking Java from small to big with JBanghandgun called jbang, showing that bazookas like maven or gradle are a<br>choice, not a necessity."/>handgun called jbang, showing that bazookas like maven or gradle are a<br>choice, not a necessity."/>handgun called jbang, showing that bazookas like maven or gradle are a<br>choice, not a necessity."/><br>_Start<br>About<br>Bio<br>Blog<br>Contact<br>Experiments<br>Links<br>Privacy<br>RSS

🌛︎︎<br>🌞︎︎

Big things have small beginnings: taking Java from small to big with JBang

Today we will tour the java jungles using one single, simple, yet powerful,<br>handgun called jbang , showing that bazookas like maven or gradle are a<br>choice, not a necessity.

But what is jbang?

Well, imagine that you want to hello world.

All you need is a compiler/interpreter and some knowledge of the language. But<br>what if you want a database hello world, or json-serialization hello world?

Unless the library for those already is present, either you write the code for<br>it yourself, from scratch, or you do as most modern languages/platforms<br>usually offers: a huge ready to use online library, just download and boom,<br>bullseye.

In Java, most people would say to you something like it's dangerous<br>to go alone, take this!, then then hand over to you a bunch of folders, a<br>cabalistic xml file and explain that this is how java goes to look like<br>for now on.

Well, this is how bureaucracy starts to enter in your veins. Now java is<br>clumsy, verbose, even slow, useless without all those devices to keep it alive.<br>But by wielding this large machinery, one can serve millions, go full<br>enterprise, to de moon, and -- no.

Java doesn't need to be that way, either too naive or too complex.

This is where JBang enters and shows that, not only the<br>Gordian Knot is easier to untie than expected, but the entire power of<br>this entire ecosystem can be tamed with less effort and bureaucracy.

But don't take my word, let's see what is possible to really do with it.

Installation

Several options:

SDKMan!

First, install sdkman

sdk install jbang<br>asdf

First, install asdf

asdf plugin-add jbang<br>asdf install jbang latest<br>asdf global jbang latest<br>You can also check a list of alternative methods to get it in the<br>project site.

Bash completon

Add the following line at the end of your .bashrc:

# JBang<br>. (jbang completion)<br>export PATH=$PATH:$HOME/.jbang/bin<br>How to hello world

Direct eval

Make jbang eval java code directly from the command line:

jbang run -c "System.out.println(\"Hello World\");"<br>Create a simple script

Use jbang to initialize a simple script

jbang init Hello.java<br>The generated file goes like this:

///usr/bin/env jbang "$0" "$@" ; exit $?<br>//JAVA 25+

void main(String... args) {<br>IO.println("Hello World");<br>If you need a specific java version, pass the version as parameter in the init<br>command:

jbang init --java=8 Hello.java<br>Then the Generated source file goes like this:

///usr/bin/env jbang "$0" "$@" ; exit $?<br>//JAVA 8

import static java.lang.System.*;

public class Hello {

public static void main(String... args) {<br>out.println("Hello World");<br>Running

Call the entry script:

jbang Hello.java<br>One cool thing is that jbang downloads a suitable jdk based on what you<br>provides in the //JAVA comment in the script.

Those comments are relevant, as we'll see over the samples.

Other exotic entry points

JBang also supports jshell scripts and markdown files as entry points, but<br>let's not cover those modes here. Just check the official docs if you want<br>to do some exotic things.

Dependency management

So far, jbang is a neat toy, but any simple java project nowadays must be able<br>to consume maven dependencies or some sort of external library registry.

It can do that too!

Creating a script with dependencies

To init a script with dependencies:

jbang init --deps=com.esotericsoftware:minlog:1.3.1 Hello.java<br>The script will look like this:

///usr/bin/env jbang "$0" "$@" ; exit $?<br>//JAVA 25+<br>//DEPS com.esotericsoftware:minlog:1.3.1

// i modified the script a little to make use of the dependency<br>import com.esotericsoftware.minlog.Log;

void main(String... args) {<br>// IO.println("Hello World");<br>Log.info("This is a tiny log message.");<br>Run it:

sombriks@erebus 03 $ jbang Hello.java<br>[jbang] Resolving dependencies...<br>[jbang] com.esotericsoftware:minlog:1.3.1<br>[jbang] Dependencies resolved<br>[jbang] Building jar for Hello.java...<br>00:00 INFO: This is a tiny log message.<br>sombriks@erebus 03 $<br>Now we're talking.

Add more dependencies

In order to add more dependencies, either provide a comma-separated list of<br>maven coordinates (or GAV as jbang refers to them), during the init<br>command or add them by hand in the script:

///usr/bin/env jbang "$0" "$@" ; exit $?<br>//JAVA 25+<br>//DEPS com.esotericsoftware:minlog:1.3.1<br>//DEPS com.google.code.gson:gson:2.11.0

import com.esotericsoftware.minlog.Log;

import com.google.gson.Gson;<br>import com.google.gson.GsonBuilder;

import java.util.ArrayList;

record Todo(String description, boolean...

jbang java hello like script world

Related Articles