DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Competing Consumers With Spring Boot and Hazelcast
  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Microservices With Apache Camel and Quarkus
  • What ChatGPT Needs Is Context

Trending

  • Competing Consumers With Spring Boot and Hazelcast
  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Microservices With Apache Camel and Quarkus
  • What ChatGPT Needs Is Context
  1. DZone
  2. Coding
  3. Languages
  4. Jbang - The Power of Shell Scripting for Java

Jbang - The Power of Shell Scripting for Java

Shell scripting for Java

Prabhu Rengaswamy user avatar by
Prabhu Rengaswamy
CORE ·
Aug. 03, 20 · Review
Like (5)
Save
Tweet
Share
7.01K Views

Join the DZone community and get the full member experience.

Join For Free

Whenever a new Java library is out, developers would be curious to try it out. However, it is sometimes cumbersome to set up the project in the IDE or as a Maven project along with the dependencies. Of course, classpaths can be set in the command line, but it is still cumbersome for the lazy developers ;-)

jbang is here to the rescue!

Setting up the project to try out a library or running a Java file is going to be a thing of the past with jbang!

Of course, Java has the JShell, but jbang seems to be better in many ways.

jbang is a neat little tool written by Max Rydahl Andersen (maxandersen) that

  • enables running java as scripts anywhere with java or jshell
  • helping to try out any Java API without the need to install, setup nor configure the project for Maven, Gradle or any other build system


A quick feature list for jbang is:

  • Requires Java 8 or above, but Java 11 is recommended
  • Dependency declarations through //DEPS group:artifact:version or @Grab annotations
  • Compile and runtime options with //JAVAC_OPTIONS <flags> and //JAVA_OPTIONS <flags>
  •  Supports the platforms - Windows, Linux and Mac
  • Transparently launch JavaFX application on Java 8 and above
  • And much more...


Now, let's quickly see the power of jbang! Let's try out a nice library — Jsoup — that is a Java library that provides very convenient APIs for fetching URLs, extracting and manipulating HTML content using DOM methods and CSS selectors, or in short JQuery like syntax to work with the DOM.

First, we will have to init the jbang class using

jbang init hellojsoup.java

This would create a hellojsoup.java file with the hellojsoup class with the main method. At the top of the file it also has commands for the Unix !shebang and the DEPS declaration.

The DEPS declaration is a comment where we specify the Gradle-like dependencies of the format — groupId:artifactId:version

In our case it will be org.jsoup:jsoup:1.13.1

Here is the code that does a simple call to Wikipedia and displays the headlines in the news section. It is a slightly modified version as given in the Jsoup site.

Java
xxxxxxxxxx
1
20
 
1
//usr/bin/env jbang "$0" "$@" ; exit $?
2
//DEPS org.jsoup:jsoup:1.13.1
3
4
import static java.lang.System.*;
5
import org.jsoup.nodes.Document;
6
import org.jsoup.Jsoup;
7
import org.jsoup.select.Elements;
8
import org.jsoup.nodes.Element;
9
10
public class hellojsoup {
11
12
    public static void main(String... args) throws Exception {
13
        Document doc = Jsoup.connect("https://en.wikipedia.org/").get();
14
        out.println(doc.title());
15
        Elements newsHeadlines = doc.select("#mp-itn b a");
16
        for (Element headline : newsHeadlines) {
17
            out.println(String.format("%s\n\t%s", headline.attr("title"),                    headline.absUrl("href")));
18
        }
19
    }
20
}



To run the code, you will have to type the following command, where the run is an optional command.

jbang run hellojsoup.java

Running this program would display the day's headlines in the news section. As of this writing, the following was the output.

Shell
x
13
 
1
Wikipedia, the free encyclopedia
2
2020 Twitter bitcoin scam
3
https://en.wikipedia.org/wiki/2020_Twitter_bitcoin_scam
4
2020 Armenian?Azerbaijani skirmishes
5
https://en.wikipedia.org/wiki/2020_Armenian%E2%80%93Azerbaijani_skirmishes
6
C/2020 F3 (NEOWISE)
7
https://en.wikipedia.org/wiki/C/2020_F3_(NEOWISE)
8
Portal:Current events
9
https://en.wikipedia.org/wiki/Portal:Current_events
10
Deaths in 2020
11
https://en.wikipedia.org/wiki/Deaths_in_2020
12
Wikipedia:In the news/Candidates
13
https://en.wikipedia.org/wiki/Wikipedia:In_the_news/Candidates



The amazing part of jbang is that it is so simple and easy. I believe it is not just to try out libraries but will grow to be a tool that will have a bigger production usage in the future as well, especially in Microservices and Serverless applications. No complex server setup, just one single Java file doing the job just fine.

It has many examples that includes vertx, undertow, JavaFX, resteasy, ratpack and much more. It also has support for all the major IDEs.

For more details and documentation, head on to https://github.com/jbangdev/jbang

jbang looks more promising and it would be no wonder if it gets included in the JDK in the future. Give it a try!

Java (programming language) shell

Published at DZone with permission of Prabhu Rengaswamy. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Competing Consumers With Spring Boot and Hazelcast
  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Microservices With Apache Camel and Quarkus
  • What ChatGPT Needs Is Context

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: