2

JBang cheatsheet (2021)

 2 years ago
source link: http://www.mastertheboss.com/java/jbang-cheatsheet-2021/?utm_campaign=jbang-cheatsheet-2021
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Here is my JBang cheatsheet which can help as a reference to code self-contained source-only Java programs with unmatched ease.

Getting started with JBang

Install JBang on Linux

curl -Ls https://sh.jbang.dev | bash -s - app setup

Create a JBang class  from a template

jbang init --template=hello helloworld.java
jbang init --template=hello helloworld.java

List of available JBang templates

jbang template list
jbang template list

Run a Java class

jbang helloworld.java
jbang helloworld.java

Update JBang

jbang version --update
jbang version --update

Running a Java class in a JAR file

jbang jgroups-4.2.15.Final.jar --main=org.jgroups.tests.McastReceiverTest
jbang jgroups-4.2.15.Final.jar --main=org.jgroups.tests.McastReceiverTest 

Running a Java class using Maven GAV coordinates

jbang org.jgroups:jgroups:5.1.9.Final --main=org.jgroups.tests.McastReceiverTest
jbang org.jgroups:jgroups:5.1.9.Final --main=org.jgroups.tests.McastReceiverTest 

Configuration

Adding dependency  

//DEPS log4j:log4j:1.2.17
//DEPS log4j:log4j:1.2.17

Adding Managed Dependencies (BOM) 

//DEPS io.quarkus:quarkus-bom:1.11.0.Final@pom
//DEPS io.quarkus:quarkus-resteasy
//DEPS io.quarkus:quarkus-smallrye-openapi
//DEPS io.quarkus:quarkus-bom:1.11.0.Final@pom
//DEPS io.quarkus:quarkus-resteasy
//DEPS io.quarkus:quarkus-smallrye-openapi

Using System properties in the comments

//DEPS org.openjfx:javafx-graphics:11.0.2:${os.detected.jfxname}
//DEPS org.openjfx:javafx-graphics:11.0.2:${os.detected.jfxname}

Using Environment variables in the comments

//DEPS org.openjfx:javafx-graphics:11.0.2:${env.jfxname}
//DEPS org.openjfx:javafx-graphics:11.0.2:${env.jfxname}

Repository for dependencies (maven repository + custom repository)

//REPOS mavencentral,acme=https://maven.acme.local/maven
//REPOS mavencentral,acme=https://maven.acme.local/maven

Adding file resources

//FILES resource.properties
//FILES META-INF/resources/index.html=index.html
//FILES resource.properties
//FILES META-INF/resources/index.html=index.html

JDK configuration

Setting JDK in your code

//JAVA 11 will force use of Java 11.
//JAVA 13+ will require at least java 13. Java 13 or higher will be used.
//JAVA 11 will force use of Java 11.
//JAVA 13+ will require at least java 13. Java 13 or higher will be used.

Install a JDK

jbang jdk install 17
jbang jdk install 17

Uninstall a JDK

jbang jdk uninstall 14
jbang jdk uninstall 14

List available JDK

jbang jdk list
jbang jdk list

Setting default JDK

jbang jdk default 12
jbang jdk default 12

Debugging and JDK Settings

Debugging a Java class

jbang --debug helloworld.java
jbang --debug helloworld.java

Setting java and javac options

//JAVAC_OPTIONS -source 11
//JAVA_OPTIONS -Xms128m -Xmx512m
//JAVAC_OPTIONS -source 11
//JAVA_OPTIONS -Xms128m -Xmx512m

Catalog

Install an app from the Catalog

jbang app install CamelJBang@apache/camel
jbang app install CamelJBang@apache/camel

List of available apps

jbang app list
jbang app list
Post Views: 155

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK