This demo illustrates how to use the new experimental WebAssembly backend for GraalVM to compile the javac tool into a Wasm module that can then run on the command-line or in the browser. Check out the live demo here.
This demo requires:
- Oracle GraalVM for JDK 25 or later.
For example, using SDKMAN!:
sdk install java 25.0.1-graal - The Binaryen toolchain in version 119 or later and on the system path.
For example, using Homebrew:
brew install binaryen
-
Build the Wasm module with the
nativeprofile:$ ./mvnw -Pnative package
The demo uses the Native Build Tools for building native images with GraalVM and Maven. This command generates a Wasm file and a corresponding JavaScript binding in the
targetdirectory. -
Compile a Java file:
node --experimental-wasm-exnref target/javac.js HelloWasm.java
The resulting
HelloWasm.classfile is placed next toHelloWasm.java. This requires Node.js 22 or later. The--experimental-wasm-exnrefoption is no longer required with Node.js 25 or later.
-
Build the Wasm module with the
javacwebprofile:./mvnw -Pjavacweb package
This command generates a Wasm file and a corresponding JavaScript binding in the
webdirectory. -
Run a local web server in the
webdirectory:cd web jwebserverThis will serve the
webdirectory locally on port8000. -
Navigate to http://localhost:8000 to compile Java programs and disassemble JVM bytecode in the browser:
