Creating my own Java CLI

Creating my own Java CLI

Java CLI

Table Of Contents

🔨 Built using Micronaut framework and GraalVM executable. CLI built using Picocli library.🔨

GradleMicronautGraalVMPicocli

Setup (SDKMan and GraalVM)

DescriptionCLI Command
Install GraalVM using sdkmansdk install java 20.1.0.r11-grl
Use GraalVmsdk use java 20.1.0.r11-grl
Install Native Image executable using GraalVM Updatergu install native-image
List GraalVM CLIgu list

Run commands

GraalVM install

Use SDKMan for easy switching between Java Version (among others)

  1. Install GraalVM-specific Java 11 JDK.

    • sdk install java 20.1.0.r11-grl
  2. Use GraalVm’s Java 11

    • sdk use java 20.1.0.r11-grl
  3. Install Native Image for native executable JARs

    • gu install native-image
  4. [Check] List images

    • gu list

Build project

./gradlew --no-daemon assemble

  • This builds Shadow JAR of the project.

CLI Commands to check JAR

  • java -jar build/libs/stackoverflow-cli-0.1-all.jar

  • java -jar build/libs/stackoverflow-cli-0.1-all.jar search

  • java -jar build/libs/stackoverflow-cli-0.1-all.jar search -q "merge maps" -t java

  • java -jar build/libs/stackoverflow-cli-0.1-all.jar search -q "merge maps" -t java --verbose

Native Binary creation

native-image --no-server -cp build/libs/stackoverflow-cli-0.1-all.jar

  • Builds the JAR to binary executable, which doesn’t require java for executing.

Use command to run the native-executable: ./stackoverflow-cli search -q "merge maps" -t java --verbose

Add Oauth2 for Stack Exchange

API Link: https://api.stackexchange.com/docs/authentication

Register API here: https://stackapps.com/apps/oauth/register

Running only auth package:

./gradlew --no-daemon run --args="auth"

Store credentials

Credentials are stored in: ~/.stackoverflow-cli/credentials.properties

Github Issues

  1. Graceful shutdown of Netty Event Loop (Micronaut’s HTTP client) takes 2 seconds. https://github.com/micronaut-projects/micronaut-core/issues/3582