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

  • JEP 330

  • Release: JDK 11

  • run .java files without javac command

    • compile bytecode into memory

  • shebang support

    • #!/usr/bin/java

    • only single-file source-code programs

πŸ“¦ Compact Source Files and Instance Main Methods

  • JEP 512

  • Release: JDK 25

  • IO.println("") instead of System.out.println("")

  • var in = IO.readln("") for CLI text input

  • no class declarator

  • auto import java.base module

  • simple main

    • void main() {}

πŸ“‚ Launch Multi-File Source-Code Programs

  • JEP 458

  • Release: JDK 22

  • run multi-file source-code programs with java command

  • execute Java file e.g. with dependencies

    • java -cp 'lib/*' Main.java

πŸ› οΈ More useful Features

πŸ”Ž 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
  • 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