JavaScript? No. Java Scripts!

Scripting with Java

π¨π»βπ» Jens Knipper
- Software Engineer
- OpenValue DΓΌsseldorf
- curious about new technologies
- likes cats πββ¬



β¨οΈ Bash

π Python


β Java
β
- statically typed
-
compiled
-
maintainability
-
performance
-
huge ecosystem
-
familiarity
β
- verbose
- boilerplate
- ceremony
not good for scripting!
is it still true? π€

π Launch Single-File Source-Code
-
Release: JDK 11
-
run
.javafiles withoutjavaccommand-
compile bytecode into memory
-
-
shebang support
-
#!/usr/bin/java -
only single-file source-code programs
-

π¦ Compact Source Files and Instance Main Methods
-
Release: JDK 25
-
IO.println("")instead ofSystem.out.println("") -
var in = IO.readln("")for CLI text input -
no class declarator
-
auto import
java.basemodule -
simple main
-
void main() {}
-

π Launch Multi-File Source-Code Programs
-
Release: JDK 22
-
run multi-file source-code programs with
javacommand -
execute Java file e.g. with dependencies
-
java -cp 'lib/*' Main.java
-

π οΈ More useful Features
-
JEP 286: Local-Variable Type Inference
varkeyword
-
-
code organization
-
-
-
simple json templates
-
-
-
HTTP requests for REST APIs etc.
-
get and parse Websites (Scraping)
-
-
-
HTTP endpoints as control interface
-

π Outlook
var json = Json.parse("""
{
"id": 1
}
""");
json.get("id").toInt();
π‘ Getting Started
- start with
void main(){} - need to organize code?
- add methods and classes
- split into multiple files
- need more functionality?
- use JDK APIs
- even more?
- add simple dependencies to classpath
- no transitive dependencies
- add simple dependencies to classpath
- even even more?
- use JBang

βΌοΈ JBang
- write and run Java scripts without the traditional setup
- goes beyond simple scripting
- launch any Java application or library packaged as a JAR
- no project setup needed
- declare dependencies with
//DEPS- automatic resolution
- generate native binaries with GraalVM
jbang --native - edit with full IDE support using
jbang edit - quick start with built-in templates
jbang init -t=... - share and reuse scripts easily
β¨οΈ CLI Applications

πΊ TUI Applications


π― Wrapping Up
- Java is great for scripting
- fooling around
- one-off scripts
- reusable scripts
- CLI applications
- TUI applications
- "smaller" standard library
- need dependencies sooner
- ecosystem compensates it
- Give it a try!

π Resources

Thank you!
Please ask questions.
βοΈ jens@openvalue.de
@jensknipper.de
π openvalue.eu

Java Scripting
By Jens Knipper
Java Scripting
- 15