Scripting Java with a jBang

Scripting Java with a jBang

JBang provides a way of running Java code as a script, similar to JShell. JBang works great on Java 8. JBang is a launcher script, written in bash and powershell. The implementation of JBang is a Java JAR archive, which it then launches to execute further commands. JBang can even run without Java being installed -- it will simply download an appropriate JVM if needed.

JBang provides a way of running Java code as a script, similar to JShell. However, unlike JShell, JBang works great on Java 8 and can be used to automatically download dependent libraries as well. In fact, JBang can even run without Java being installed -- it will simply download an appropriate JVM if needed.

JBang is a launcher script, written in bash and powershell, that can discover or download a JVM, and then (down)load the Java script given in an argument. The implementation of JBang is a Java JAR archive, which it then launches to execute further commands.

JBang can run jsh or java files; the latter is a standard Java class with a main() method. However, unlike JShell, comments at the top of JBang allow dependencies to be automatically downloaded and set up on the classpath. JShell allows adding JARs to the classpath at launch, but any (recursive) dependencies have to be added manually.

For example, to run a Java application that prints big text using the (GPL) jfiglef dependency, you can execute the following script with JBang:

//usr/bin/env jbang "$0" "[email protected]" ; exit $?
//DEPS com.github.lalyos:jfiglet:0.0.8
import com.github.lalyos.jfiglet.FigletFont;
class hello {
  public static void main(String... args) throws Exception {
    System.out.println(FigletFont.convertOneLine("Hello InfoQ");
  }
}

After installing jbang with sdkman or by downloading it, running jbang run hello.java InfoQ will print:

  _   _          _   _             ___            __            ___  
 | | | |   ___  | | | |   ___     |_ _|  _ __    / _|   ___    / _ \ 
 | |_| |  / _ \ | | | |  / _ \     | |  | '_ \  | |_   / _ \  | | | |
 |  _  | |  __/ | | | | | (_) |    | |  | | | | |  _| | (_) | | |_| |
 |_| |_|  \___| |_| |_|  \___/    |___| |_| |_| |_|    \___/   \__\_\

java programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Top 10+ Java Development Companies in India & Developers Reviews 2020 – TopDevelopers.co

A thoroughly researched list of top 10 Java development companies in India with ratings & reviews to help find the best java developers around India.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

3 Best Programming Languages for Java Developers

In this article, I’ll share 3 JVM Programming languages Java programmers can learn and why you should learn them. Being a Polyglot developer is of good quality and often valued highly in interviews.

Developer Career Path: To Become a Team Lead or Stay a Developer?

For a developer, becoming a team leader can be a trap or open up opportunities for creating software. Two years ago, when I was a developer, ... by Oleg Sklyarov, Fullstack Developer at Skyeng company