Multiple Languages
JBang supports multiple languages beyond Java, making it a versatile tool for JVM-based scripting. Here’s how to use each supported language type.
JShell Scripts (.jsh)
JShell files offer a more interactive scripting experience without needing a full class structure.
Basic JShell Usage
Create a file called hello.jsh:
///usr/bin/env jbang "$0" "$@" ; exit $?
// No class or main method needed!
System.out.println("Hello " + (args.length > 0 ? args[0] : "World"));
Run it:
jbang hello.jsh
# Output: Hello World
jbang hello.jsh JBang
# Output: Hello JBang
Arguments and System Properties
JShell scripts automatically have access to:
-
String[] args- Command line arguments -
System properties set with
-Dkey=value
///usr/bin/env jbang "$0" "$@" ; exit $?
System.out.println("Hello " + (args.length > 0 ? args[0] : "World"));
System.out.println("Debug mode: " + System.getProperty("debug", "false"));
Run with properties:
jbang -Ddebug=true hello.jsh JBang
# Output:
# Hello JBang
# Debug mode: true
|
Boolean Properties: Use
|
Kotlin Scripts (.kt) [EXPERIMENTAL]
JBang supports Kotlin scripts using the Kotlin compiler.
Basic Kotlin Usage
Create a file called hello.kt:
///usr/bin/env jbang "$0" "$@" ; exit $?
//KOTLIN 2.0.21
fun main(args: Array<String>) {
println("Hello ${args.firstOrNull() ?: "World"}")
}
Run it:
jbang hello.kt
# First time: Downloads and installs Kotlin
# Output: Hello World
jbang hello.kt JBang
# Output: Hello JBang
Kotlin with Dependencies
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.jetbrains.kotlin:kotlin-stdlib:2.0.21
//DEPS com.fasterxml.jackson.module:jackson-module-kotlin:2.15.2
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
data class Person(val name: String, val age: Int)
fun main(args: Array<String>) {
val mapper = jacksonObjectMapper()
val person = Person("Alice", 30)
val json = mapper.writeValueAsString(person)
println("JSON: $json")
val parsed = mapper.readValue<Person>(json)
println("Parsed: $parsed")
}
Groovy Scripts (.groovy) [EXPERIMENTAL]
JBang supports Groovy scripts using the Groovy compiler.
Basic Groovy Usage
Create a file called hello.groovy:
///usr/bin/env jbang "$0" "$@" ; exit $?
//GROOVY 3.0.9
def name = args.length > 0 ? args[0] : "World"
println "Hello $name"
Run it:
jbang hello.groovy
# First time: Downloads and installs Groovy
# Output: Hello World
jbang hello.groovy JBang
# Output: Hello JBang
Markdown Scripts (.md) [EXPERIMENTAL]
JBang can extract and execute Java code from Markdown files!
Basic Markdown Usage
Create a file called readme.md:
# My Script Documentation
This is a script written in Markdown that JBang can execute.
```java
class Demo {
void greet() {
System.out.println("Hello from Markdown!");
}
}
```
```jshelllanguage
new Demo().greet();
```
You can even use dependencies:
```jsh
//DEPS com.github.lalyos:jfiglet:0.0.8
import com.github.lalyos.jfiglet.FigletFont;
System.out.println(FigletFont.convertOneLine(
"Hello " + ((args.length > 0) ? args[0] : "jbang")
));
```
Arguments work too:
```java
if(args.length == 0) {
System.out.println("You have no arguments!");
} else {
System.out.printf("You have %s arguments! First is %s%n", args.length, args[0]);
}
```
Run it:
jbang readme.md YOLO
# Output: ASCII art "Hello YOLO" and argument info
How Markdown Execution Works
-
Code Block Extraction: JBang finds
java,jsh, orjshelllanguagecode blocks -
Dependency Resolution: Processes
//DEPSstatements in code blocks -
Execution Decision: If a main method is found, treats as Java; otherwise uses JShell
-
Argument Passing: Makes
argsavailable to all code blocks
Language-Specific Features
Best Practices
Choose the Right Language
-
Java: For production scripts, complex logic, performance-critical code
-
JShell: For quick experiments, interactive exploration, simple scripts
-
Kotlin: For modern language features, null safety, functional programming
-
Groovy: For dynamic scripting, text processing, DSLs
-
Markdown: For documentation, tutorials, literate programming
Performance Considerations
Compilation vs. Interpretation:
-
Java/Kotlin/Groovy: Compiled and cached (faster execution)
-
JShell: Interpreted (faster development)
-
Markdown: Depends on extracted code
Startup Time:
-
Java: Fastest after first compilation
-
JShell: Moderate
-
Kotlin/Groovy: Slower first time (compiler download)
Common Issues and Solutions
What’s Next?
-
Learn about dependencies → Dependencies
-
Explore execution options → Execution Options
-
Share your scripts → Aliases & Catalogs
Choose the language that fits your needs and start scripting! 🚀