Sitemap

Wasm(WebAssembly) with Java and GraalVM

7 min readMar 30, 2025

An Introduction to WebAssembly with a Java Demo Compiled Using GraalVM

Press enter or click to view image in full size
Photo by Towfiqu barbhuiya on Unsplash

What is Wasm (WebAssembly)?

WebAssembly is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.

It describes a memory-safe, sandboxed execution environment that may even be implemented inside existing JavaScript virtual machines.

WebAssembly modules will be able to call into and out of the JavaScript context and access browser functionality through the same Web APIs accessible from JavaScript. It also supports non-web embeddings.

When embedded in the web, it will enforce the same-origin and permissions security policies of the browser.

While it is designed to run on the Web, it is also desirable for it to be able to execute well in other environments, including application environments e.g. on servers in datacenters, on IoT devices, or mobile/desktop apps.

It defines a text format to be rendered when developers can view the source of a WebAssembly module in any developer tool. This is suitable for debugging, testing and writing programs by hand.

To check if you browser supports WebAssembly features, please visit the following:

Some features may require turning on or off by the user of the browser.

There is also “WASM.io” which is a conference focused on the WebAssembly ecosystem and in WASM I/O 2025 (27–28 Mar, Barcelona), there has been a presentation called “The Future of Write Once, Run Anywhere: From Java to WebAssembly” by

and Patrick Ziegler:

Here is a demo page (from the conference) where you can turn Java source code into JVM bytecode, which you can then download or disassemble:

Press enter or click to view image in full size

You may see Compile button as disabled since your browser does not support (or the flags are not enabled) WebAssembly features. I am currently using Vivaldi (and I attached the about page as a screenshot below for you to see the version) and I did not encounter any problems running the demo page.

Press enter or click to view image in full size

Following the steps in

s post, we will now try to write a sample Java code and target WebAssembly. For now, it will be supported by Oracle GraalVM (as opposed to GraalVM community edition) Java 25. It is just a start and still requires lots of work to be done (e.g. there is no networking support).

You can read an older blog post of mine from 2022, about GraalVM:

We will be using SDKMAN:

curl -s "https://get.sdkman.io" | bash

Now run the following for the changes to take effect:

source "$HOME/.sdkman/bin/sdkman-init.sh"

For bash, SDKMAN appends the required export paths to your “~/.bashrc” file. If you are using zsh, please run the following for the changes to take effect globally:

echo 'source "$HOME/.sdkman/bin/sdkman-init.sh"' >> ~/.zshrc
source ~/.zshrc

Run the following to check if the installation is successful:

sdk version

You can install the latest stable version of Java with the following command (we do not need to do this right now, though):

sdk install java

The installed jdks can be found at path “$HOME/.sdkman/candidates/java/”. You can echo $JAVA_HOME as well:

echo $JAVA_HOME

You can now check the current java version in the terminal:

sdk current java

You can list the available versions with the following command:

sdk list java

You can see which versions are installed with the following command:

sdk list java | grep installed
Press enter or click to view image in full size

You can see the “>>>” indicator to show the version in use.

Since there doesn’t yet have a distribution for this early access build of GraalVM for Java 25, we will perform a couple of manual steps.

You can download a bash script from here, for the installation of the supported Java version. Depending on whether you’re on macOS or Linux and x86 or ARM, it will download the compatible version.

Navigate to the folder where the newly downloaded file exists and run:

./setup.sh
Press enter or click to view image in full size

The compatible version downloads but gets an error while moving:

Press enter or click to view image in full size

I changed the script a little bit to make it work (I am a Mac user); you can find it here:

Voilà!

Press enter or click to view image in full size

Now, we have Oracle GraalVM for Java 25 installed and to make it the default version globally, you can run:

sdk default java 25.ea.15-graal

Check the result:

java --version
Press enter or click to view image in full size

Create a Hello World sample code by creating a “Main.java” file with the following content:

public class Main {
public static void main(String[] args) {
String name = (args.length > 0) ? args[0] : "World";
System.out.println("Hello, " + name + "!");
}
}

It is an easy piece of code that takes name as a command-line argument for the greeting and if it does not exist, then defaults to “World”.

Compile and run it with the following (you can change the parameter to your name):

javac Main.java && java Main Nil
Press enter or click to view image in full size

To make this work in WebAssembly. But first, you’ll need a set of tools in a package called binaryen.

Binaryen is a compiler and toolchain infrastructure library for WebAssembly, written in C++. It aims to make compiling to WebAssembly easy, fast, and effective.

You can install it using Homebrew:

brew install binaryen
Press enter or click to view image in full size

Native Image is a technology to compile Java code ahead-of-time to a binary — a native executable. To get a GraalVM native image, you should run the following command:

javac Main.java && native-image Main

Here is the output:

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

And this is the screenshot of the directory:

Later run the following (modify the parameter to be your name) in the current directory (where your Main.java file resides) and check if it works:

./main Nil

If all is well, then it seems we are now ready to get a WebAssembly artifact:

javac Main.java && native-image --tool:svm-wasm Main

Here is the output:

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

And here is the updated directory screenshot containing newly generated files (we can see .js files added):

We are now ready to run “main.js” file with Node.js. You need node v22 or higher.

Just like SDKMAN, we can use NVM. You can install it with Homebrew:

brew install nvm

Copy and paste the following to your “~/.zshrc” file if you are using zsh. If you are using bash, please add it to “~/.bash_profile” file.

export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm"

Be careful to keep SDKMAN config at the end of the file:

Press enter or click to view image in full size

For the changes to take effect in the current terminal window, please run the first if you are using zsh; the latter if you are using bash:

source ~/.zshrc

source ~/.bash_profile

Check if the installation is successful:

nvm version

You can install the latest version:

nvm install node

You can list available versions with:

 nvm ls-remote

You can list installed versions on your Mac with the following command:

nvm list

Now, run the following after changing the parameter to your name (in the current directory where the generated .js files reside):

node main.js Nil

Voilà!

Happy Coding!

Giphy

References:
https://webassembly.org
Josh Long — Coffee + Software — Build WebAssembly with Java?? — https://www.youtube.com/watch?v=VqhO3AkB-2M
https://joshlong.com/jl/blogPost/my-first-java-script-script-with-graalvm-and-webassembly.html

--

--

Nil Seri
Nil Seri

Written by Nil Seri

I would love to change the world, but they won’t give me the source code | coding 👩🏻‍💻 | coffee ☕️ | jazz 🎷 | anime 🐲 | books 📚 | drawing 🎨

No responses yet