Scripting is not something you do with Java. You use bash, python, groovy, kotlin, or something else other than Java. What if I told you that it does not need to be that way? Since January 2020 there has been a way to use Java as a language for scripting. No maven, no gradle, no scaffolding–just a single .java file that can use external libraries and you are off the races. This session will cover how jbang came to be, what challenges it solves, and how it helps you to explore and teach Java.
Speaker: Max Rydahl Andersen
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
jbang: Unleash the power of Java for shell scripting
1. Unleash the power of Java for shell scripting
by
Max Rydahl Andersen
@maxandersen
https://jbang.dev
2.
3. Why Java as a scripting language makes sense
Top #1-#2 language in the world!
Kotlin and Groovy does it, why not Java ?
Content assist and refactoring in all major IDE’s and text editors
Debugging! Monitoring!
Recent Java language features (var, <>, lambda, text blocks, …)
Streams and more system level access in JDK
Massive ecosystem of 3rd party dependencies
BUT Maven, Gradle, etc. should NOT be required to start
7. “Everything should be made as
simple as possible,
but not simpler”
- (maybe) Albert
Einstein
8. hello.java:
class hello {
public static void main(String... args) throws Exception {
System.out.println(
"Hello " +
((args.length>0)?args[0]:"java"));
}
}
Java 1-9
Manually need to set classpath
to use 3rd party dependencies
2 steps that should be one!
9. Java 9 (introduces jshell)
hello.jsh:
System.out.println("Hello);
Awesome! No main!
No way to handle arguments!
Kinda Breaks
in IDE’s
10. class hello {
public static void main(String... args) throws Exception {
System.out.println(
"Hello " +
((args.length>0)?args[0]:"java"));
}
}
Java 10+ Module system but still
manually configure classpath
11. hello:
#!/usr/bin/env java --source 11
class hello {
public static void main(String... args) throws Exception {
System.out.println(
"Hello " +
((args.length>0)?args[0]:"jbang"));
}
}
Java 11+ Cannot be called .java
#! works
but must have --source 11
Breaks every
IDE :(
12. Java 1-8 Java 10 Java 11
1 Compile & build in one
2 External Dependencies
3 Executable scripts
(./hello)
5 (Easy) Debugging
4 IDE support
26. hello.java:
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS https://github.com/lalyos/jfiglet
import com.github.lalyos.jfiglet.FigletFont;
class hello {
public static void main(String... args) throws Exception {
System.out.println(FigletFont.convertOneLine(
"Hello " + ((args.length>0)?args[0]:"jbang")));
}
}
Use github repos as
dependencies (snapshot,
branches & tags)
27. //REPOS jboss,google
//REPOS acme=https://maven.acme.local/maven
@GrabResolver("mavenCentral") // (2)
@GrabResolver(name='acme', root='https://maven.acme.local/maven')
@Grapes({ // (3)
@Grab(group="org.codehaus.groovy", module="groovy", version="2.5.8"), // (4)
@Grab(module = "log4j", group = "log4j", version = "1.2.17")
})
Easy add common
maven repositories
Use custom incl.
Secured repoistories
Use Groovy style @Grab
28. //usr/bin/env jbang "$0" "$@" ; exit $?
//JAVAC_OPTIONS --enable-preview -source 14
//JAVA_OPTIONS --enable-preview
import static java.lang.System.*;
public class records {
record Point(int x, int y) {}
public static void main(String[] args) {
var p = new Point(2,4);
out.println(p);
}
}
Try out latest greatest preview features
32. How do I get it ?
https://jbang.dev/download
Windows
Linux
Mac
Docker/Container
Github Action
33. Go Use JBang!
● Use it for your automation needs
● One-off scripts for fixing things
● When trying out new apis
● In docs showing how to use your api
● In issues when making a reproducer
● When teaching newcomers to java