Java
Java is a high-level, general-purpose, memory-safe, object-oriented programming language.
In LiveCodes, Java source is compiled to WebAssembly in the browser: a build of OpenJDK's javac
and the TeaVM AOT compiler run as a single WebAssembly module inside a Web
Worker. Nothing is sent to a server, and modern Java (records, var, text blocks, switch
expressions) is supported.
The language is named java-wasm; java is an alias for it (it used to select the Java 8 engine,
which is still available as java-8).
Usage
Demo:
Communication with JavaScript
The Java code runs in the context of the result page.
A few helper properties and methods are available in the browser global livecodes.java object:
livecodes.java.input: the initial standard input that is passed to the Java code.livecodes.java.loaded: A promise that resolves when the Java environment is loaded. Any other helpers should be used after this promise resolves.livecodes.java.output: the standard output.livecodes.java.error: the standard error.livecodes.java.exitCode: the exit code.livecodes.java.run: a function that runs the Java code with new input. This function takes a string as input and returns a promise that resolves when the Java code is done running. The promise resolves with an object containing theinput,output,error, andexitCodeproperties.
Example:
Language Info
Name
java-wasm (alias: java)
Extension
.java
Editor
script
Compiler
A fork of teavm-javac packaging OpenJDK's javac and
the TeaVM AOT compiler as WebAssembly, run in a Web Worker by
@live-codes/java-wasm.
Version
teavm-javac built on TeaVM 0.14.1, with javac from OpenJDK 25.
Code Formatting
Using @wasm-fmt/clang-format, applying Google's Java style guide.
Live Reload
By default, new code changes are sent to the result page for re-evaluation without a full page reload, to avoid the need to reload the Java environment.
This behavior can be disabled by adding the code comment // __livecodes_reload__ to the code, which will force a full page reload.
This comment can be added in the hiddenContent property of the editor for embedded playgrounds.
Starter Template
https://livecodes.io/?template=java