Getting Started with Java

Max Rydahl Andersen
@maxandersen
redhat

What is hard about getting started with Java ?

reddit/learnjava

reddit comparehelloworldpython

reddit/learnjava

reddit installingjava

reddit/learnjava

reddit runningthedamnthing

Getting Started with Java

The JBang Way

Installation

curl -Ls https://sh.jbang.dev | bash -s - app setup
  • Downloads and configure JBang

  • If no Java found, downloads Java 11

Everywhere

$ jbang -c 'println("Hello World")'
Hello World
$ jbang -c 'println("Hello "  + args[0])' Universe
Hello Universe
  • -c evaluates code via JShell

  • Handles user arguments

  • No files to create

  • No public static class main

$ jbang -i
|  Welcome to JShell -- Version 11.0.15
|  For an introduction type: /help intro

jshell> println("Hello!")
Hello

Any Java

$ jbang --java 17 -i
WARNING: Using incubator modules: jdk.incubator.vector, jdk.incubator.foreign
|  Welcome to JShell -- Version 17
|  For an introduction type: /help intro

jshell>

Files

$ jbang init hello.java
[jbang] File initialized. You can now run it with
'jbang hello.java' or edit it using
'jbang edit --open=[editor] hello.java' where [editor]
is your editor or IDE, e.g. 'eclipse'
$ jbang hello.java
[jbang] Building jar...
Hello World
$ ./hello.Java
Hello World

Init w/Templates

$ jbang init -t cli app.java
[jbang] File initialized. You can now run it with
'jbang app.java' or edit it using
'jbang edit --open=[editor] app.java' where [editor]
is your editor or IDE, e.g. 'eclipse'

app.java

 ///usr/bin/env jbang "$0" "$@" ; exit $?
 //DEPS info.picocli:picocli:4.5.0

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;

import java.util.concurrent.Callable;

@Command(name = "app", mixinStandardHelpOptions = true, version = "app 0.1",
        description = "app made with jbang")
class app implements Callable<Integer> {

    @Parameters(index = "0", description = "The greeting to print", defaultValue = "World!")
    private String greeting;

    public static void main(String... args) {
        int exitCode = new CommandLine(new app()).execute(args);
        System.exit(exitCode);
    }

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        System.out.println("Hello " + greeting);
        return 0;
    }
}

Run with Dependencies

$ ./app.java
[jbang] Resolving dependencies...
[jbang] info.picocli:picocli:jar:4.5.0
Done
[jbang] Dependencies resolved
[jbang] Building jar...
Hello World

Cached Run

$ ./app.java --help
Usage: app [-hV] <greeting>
app made with jbang
      <greeting>   The greeting to print
  -h, --help       Show this help message and exit.
  -V, --version    Print version information and exit.

Editing

The JBang Way

The Simplest Way

jbang edit app.java
[jbang] You requested to open default editor but no default
editor configured.

jbang can download and configure a visual studio code (VSCodium)
with Java support to use

Do you want to

(1) Download and run VSCodium
(2) Use 'code'
(3) Use 'eclipse'
(4) Use 'idea'
(0) Cancel

Anywhere

Direct IDE Support

IntelliJ intellij

Eclipse and Visual Studio Code In progress

Debugging

The JBang Way

$ jbang --debug app.java
Listening for transport dt_socket at address: 4004

What just happened?

The JBang Way

In 5 min or less…​

We started creating, then edited, ran and debugged Java

  • Without java or javac

  • Witout mvn or gradle

  • No vscode, eclipse, intellij Install

  • Just jbang

Exploring…​

The JBang Way

[background

One liner scripts

$ cat 5letterwords.txt | jbang -c \
  'lines().filter(s->s.contains("dog")).forEach(p->println(p))'

Microservices w/Quarkus

$ jbang init -t qrest myservice.java
$ ./myservice.java
./myservice.java
[jbang] Resolving dependencies...
[jbang] Artifacts used for dependency management:
         io.quarkus:quarkus-bom:pom:1.11.0.Final
[jbang] io.quarkus:quarkus-resteasy
Done
[jbang] Dependencies resolved
[jbang] Building jar...
[jbang] Post build with io.quarkus.launcher.JBangIntegration
May 04, 2022 9:30:43 PM org.jboss.threads.Version <clinit>
INFO: JBoss Threads version 3.2.0.Final
May 04, 2022 9:30:44 PM io.quarkus.deployment.QuarkusAugmentor run
INFO: Quarkus augmentation completed in 999ms
__  ____  __  _____   ___  __ ____  ______
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2022-05-04 21:30:45,453 INFO  [io.quarkus] (main) Quarkus 2.8.0.Final on JVM started in 0.839s. Listening on: http://0.0.0.0:8080
2022-05-04 21:30:45,488 INFO  [io.quarkus] (main) Profile prod activated.
2022-05-04 21:30:45,488 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy]

myservice.java

///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.quarkus:quarkus-bom:${quarkus.version:1.11.0.Final}@pom
//DEPS io.quarkus:quarkus-resteasy
//JAVAC_OPTIONS -parameters
//FILES resources/index.html=index.html
//SOURCES **.java

import io.quarkus.runtime.Quarkus;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/hello")
@ApplicationScoped
public class myservice {
    @GET
    public String sayHello() {
        return "Hello from Quarkus with jbang.dev";
    }
}

Run All The Things

$ jbang -c "Hello"
$ jbang app.java
$ jbang demo.jsh
$ jbang app.jar
$ jbang org.jreleaser:jreleaser:1.0.0
$ jbang https://github.com/jbangdev/jbang/../examples/inetTest.java
$ jbang https://jbang.dev

Sharing is Caring

  • jbang-catalog repository or jbang-catalog.json

  • Run aliases: jbang env@jbangdev, jbang minecraft-server@microsoft

  • Init templates: jbang init -t githubapp@quarkusio myapp.java

  • Browse on jbang.dev/appstore

JBang AppStore

faker.jsh Example

//DEPS com.github.javafaker:javafaker:1.0.2

import com.github.javafaker.Faker;

Faker faker = new Faker();
$ jbang -s faker@jbangdev -c \
  'Stream.generate(faker.name()::fullName).filter(s->s.contains("Angie")).forEach(s -> println("Awesome " + s))'

Install All The Things

$ jbang app install -n myapp https://.../a.jar
$ jbang app install quarkus@quarkusio
$ quarkus
quarkus

Quarkus CLI version 2.8.2.Final

Create Quarkus projects with Maven, Gradle, or JBang.
Manage extensions and source registries.

Create: quarkus create
Iterate: quarkus dev
Build and test: quarkus build

Find more information at https://quarkus.io
If you have questions, check https://github.com/quarkusio/quarkus/discussions


Usage: quarkus [-ehv] [--verbose] [-D=<String=String>]... [COMMAND]
Options:

Conclusion

  • JBang is the best way to Get started with Java

  • JBang let you write anything; from one-liners…​

  • …​to full-fledged Java microservices

  • JBang lets you run and install Java apps

  • JBang has an appstore

The End

jbang.dev jbang.dev/appstore …​works with Java, Kotlin and Groovy too!

Slides: GitHub
Built with JBang, AsciiDoctor and reveal.js
Images are various Mandalorian fan-art.