Examples · Java
Run Java code without a class or main()
A Java snippet normally needs a class around it and a public static void main before it will run. ByteRunner wraps the statements for you: write them, press F5, and see the results.
This one declares a record, filters a list with a stream, and counts books per year with Collectors.groupingBy into a TreeMap.
The query
java-without-main.java · paste it into a Java tab and press F5, or run byterunner run java-without-main.java in a terminal
// Plain Java statements: no class, no public static void main.
// java.util, java.util.stream, java.time and a few more are imported already.
record Book(String title, String author, int year) {}
var books = List.of(
new Book("The Pragmatic Programmer", "Hunt & Thomas", 1999),
new Book("Effective Java", "Bloch", 2001),
new Book("Refactoring", "Fowler", 1999),
new Book("Clean Architecture", "Martin", 2017));
dump(books.stream().filter(b -> b.year() < 2010).toList(), "Before 2010");
var byYear = books.stream()
.collect(Collectors.groupingBy(Book::year, TreeMap::new, Collectors.counting()));
dump(byYear, "Books per year");
What it shows
The output of that run, from byterunner run. In the app each table is a sortable grid.
Before 2010
title author year
------------------------ ------------- ----
The Pragmatic Programmer Hunt & Thomas 1999
Effective Java Bloch 2001
Refactoring Fowler 1999
(3 rows)
Books per year
Key Value
---- -----
1999 2
2001 1
2017 1
(3 rows)
java.util, java.util.stream, java.util.function, java.util.regex, java.math, java.time, java.io, java.nio.file and java.net are imported already. Anything else is imported at the top as usual.
Records, var, switch expressions and the rest of modern Java work, and ByteRunner brings its own Java, so nothing needs installing first. To write a full program instead, switch the query to Program mode and give it a main().