Cheatography
https://cheatography.com
Unleash the power of Java https://www.jbang.dev/
Simple JBang Hello.java
///usr/bin/env jbang "$0" "$@" ; exit $?
class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
|
Run JBang script
$ jbang Hello.java
$ jbang --debug Hello.java
$ chmod u+x
$ ./Hello.java
|
Templates
$ jbang template list
$ jbang init --template=cli helloworld.java
|
Editing
$ jbang edit helloworld.java
$ jbang edit --open=code helloworld.java
$ jbang edit --open=idea helloworld.java
Assign Java version
//JAVA 8 |
Assigned version in Script |
//JAVA 11+ |
Java 11 or higher |
jbang --java 8 Hello.java |
Assigned version from CLI |
java and javac Options
//JAVAC_OPTIONS --enable-preview --release 17
//JAVA_OPTIONS --enable-preview
//JAVA_OPTIONS -Dserver.port=8081 -D spring.application.name=app1
|
Multiple Java source files
//SOURCES UserService.java
//SOURCES impl/UserServiceImpl.java
|
Multi resource files - properties/xml etc
//FILES application.properties
//FILES META-INF/resources/index.html=index.html
//FILES META-INF/resources/jquery.js=https://code.jquery.com/jquery-3.6.0.min.js
Resource import format: //FILES <mountpoint>[=<sourcefile>]
|
Polyglot - Kotlin
///usr/bin/env jbang "$0" "$@" ; exit $?
//KOTLIN 1.6.10
//DEPS org.jetbrains.kotlin: kotlin-stdlib-jdk8:1.6.10
public fun main() {
println("Hello World");
}
|
Polyglot - Groovy
///usr/bin/env jbang "$0" "$@" ; exit $?
//GROOVY 4.0.0
println("Hello World");
|
Polyglot - JShell
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.github.javafaker:javafaker:1.0.2
import com.github.javafaker.Faker;
Faker faker = new Faker();
|
$ jbang demo.jsh
$ jbang --interactive demo.jsh
|
|
Dependencies
//DEPS org.projectlombok:lombok:1.18.22
//DEPS org.apache.commons:commons-lang3:3.12.0
|
Managed dependencies by BOM - Spring Boot
//DEPS org.springframework.boot:spring-boot-dependencies:2.6.3@pom
//DEPS org.springframework.boot:spring-boot-starter
|
Managed dependencies by BOM - Quarkus
//DEPS io.quarkus:quarkus-bom:2.7.1.Final@pom
//DEPS io.quarkus:quarkus-resteasy
|
Catalogs - jbang-catalog.json
{
"aliases": {
"hello": {
"script-ref": "hello.java",
"description": "Script that says hello back for each argument"
}
}
|
Catalogs - local jbang-catalogs.json
1. jbang-catalog.json in current directory |
2. jbang-catalog.json in parent directory util the root diretory |
3. $HOME/.jbang/jbang-catalog.json |
Catalogs - implicit catalog from GitHub
jbang hello@acme |
hello alias in acme/jbang-catalog/jbang-catalog.json |
jbang hello@acme/mycatalog |
hello alias in acme/mycatalog/jbang-catalog.json |
jbang hello@acme/mycatalog/dev |
hello alias in acme/mycatalog/jbang-catalog.json in dev branch |
jbang hello@acme~experimental |
hello alias in acme/jbang-catalog/experimental/jbang-catalog.json |
Catalogs - management
$ jbang catalog add --name acme acme
$ jbang catalog add --name mycatalog acme/mycatalog
$ jbang catalog list
|
Tips
jbang --fresh |
Use none-cached resources |
jbang info tools Hello.java |
Prints a json description of JBang script |
Apps - setup jbang to put scripts into your PATH
$ jbang app install <scriptRef>
$ $ jbang app install --name app1 <scriptRef>
$ jbang app list
$ jbang app uninstall <name>
|
build.java
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17
//DEPS info.picocli:picocli:4.6.3
//SOURCES src
//FILES resources
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
More Cheat Sheets by linux_china