Creating my own Java CLI
Java CLI
Table Of Contents
🔨 Built using Micronaut framework and GraalVM executable. CLI built using Picocli library.🔨
Setup (SDKMan and GraalVM)
| Description | CLI Command |
|---|---|
| Install GraalVM using sdkman | sdk install java 20.1.0.r11-grl |
| Use GraalVm | sdk use java 20.1.0.r11-grl |
| Install Native Image executable using GraalVM Updater | gu install native-image |
| List GraalVM CLI | gu list |
Run commands
GraalVM install
Use SDKMan for easy switching between Java Version (among others)
-
Install GraalVM-specific Java 11 JDK.
sdk install java 20.1.0.r11-grl
-
Use GraalVm’s Java 11
sdk use java 20.1.0.r11-grl
-
Install Native Image for native executable JARs
gu install native-image
-
[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
- Graceful shutdown of Netty Event Loop (Micronaut’s HTTP client) takes 2 seconds. https://github.com/micronaut-projects/micronaut-core/issues/3582



