Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News JBang 0.107.0 Delivers Java 21 Preview, OpenAI Integration, and More

JBang 0.107.0 Delivers Java 21 Preview, OpenAI Integration, and More

The latest JBang release provides a preview of Java 21 and more integration with the OpenAI plugin via the quickstart port to Java. JBang has also recently introduced the integration of ChatGPT via the CLI, allowing developers to create JBang scripts using ChatGPT.

JBang is a lightweight Java-based scripting tool that allows developers to write and run Java code directly from the command line without requiring an entire Java project or compilation steps. It was developed to simplify the execution of Java code more interactively and conveniently, similar to languages like Python.

Java developers can now execute Java code with JBang using the upcoming release of Java 21, scheduled for September 19, 2023. Furthermore, developers can also request via CLI to generate code with OpenAI’s ChatGPT or even create plugins in Java for ChatGPT.

To use JBang with Java 21 preview features, simply run the following command:

$ jbang --java 21 --enable-preview -c 'print(STR."First arg is \{args[0]}!")' JBang

The command will download the early-access release of OpenJDK 21 in case there isn't one on the system. Furthermore, it will allow Java developers to use the preview features like String templates.

The enhancements introduced also bring some exciting ergonomics, e.g., a developer doesn't necessarily need to figure out all the intrinsic details of an API. In the following example, the command creates Java code that takes an image, adds text, and then writes it to the filesystem. Usually, anyone learning OpenCV in Java would need to know the intrinsic details of image processing and its components and concepts to understand dependencies before even beginning to write any code.

$ jbang --preview --verbose init "Write a Java program that processes an input image and add text Hello world on it using OpenCV to produce an output image called output.png"

In the above command:

  • The --preview argument for using preview features, e.g., ChatGPT
  • The init argument will create a new file in the directory where the command executes.
  • The text "Write..." requests to create a Java program that uses OpenCV.

Other mundane tasks, for example calling a REST API via a Java Client and showing the results, have become single-line commands.

$ jbang init "print a random chuck norris joke fetched from the API service providing free jokes API."

$ jbang

Or to fetch information and process it further:

$ jbang --verbose init "connect to GitHub and fetch all repositories under jbang and collect how many stars they have"

In conjunction with the 0.107.0 release, Max Andersen, a distinguished engineer at Red Hat and creator of JBang, also introduced a port for the OpenAI plugin Quickstart, allowing users to write plugins for ChatGPT and test them locally with JBang.

InfoQ spoke to Max Andersen on this release and the future of Java and AI.

InfoQ: What does JBang integration to OpenAI’s ChatGPT bring to the Java community?

Max Andersen: JBang today has default templates for creating a starting Java project. However, one still needs to know their names and what they do; secondly, they are limited. With JBang OpenAI support, you can use natural language to outline your ideas and wishes and get a good starting point. This is much faster than doing a web search to try and collect all the required components. Since JBang has always been about reducing the ceremony around Java projects, this was a natural extension of JBang. It's a great "cure" against writer's block – it lets me get started. It lets anyone get started with Java as a newbie or as an expert and lets you try new things.

InfoQ: Does the Java community need more integration into Artificial Intelligence libraries and tools?

Andersen: Python is dominant in the data modeling and processing parts of Artificial Intelligence – and for a good reason, as Python is very good as a data manipulation DSL. However, Java can shine in being used to do things way more efficiently and utilize the models coming out of AI. In this case, loading models using native code or even, in the future, pure Java would be helpful.There is also a lot of cross-pollination from which the Java and Python ecosystems could benefit. For example, I recently realized that Jupyter Notebooks could be used as a basis for Java/JBang tutorials.

InfoQ: Are there any specific features or integrations you are working on for AI?

Andersen: For JBang itself, similar to what was done with the jbang init command, I haven't explored much more yet, besides upgrading it to GPT-4 instead of GPT-3. I've used OpenAI in a Quarkus CLI command called quarkus explain that allows you to use OpenAI in a project and ask it to explain individual files, whether that is .java, pom.xml, or an file. It works remarkably well.

InfoQ: What's next for JBang and Artificial Intelligence?

Andersen: I used JBang to make an OpenAI plugin using Quarkus: I was quite pleased seeing that it reduces the code by about 30-40% compared to similar Python code. I want to use the JBang approach to illustrate Java is not as verbose and complex as many think. Java has matured a lot since the early 2010s.

Java and Artificial Intelligence (AI) are becoming better integrated, and Java core is moving in a direction that further supports some of these ideas. The recent inclusion of the Foreign Function & Memory API (Third Preview) and the Vector API (Sixth Incubator) is a step towards integrating with other libraries and enabling further innovation in this space.

About the Author

Rate this Article