javawebassembly

How to compile Java to WASM (WebAssembly)?


I wonder can I use Java and compile it to WASM (WebAssembly)?

The list https://webassembly.org/getting-started/developers-guide/ has no Java, but Kotlin


GraalVM WASM project https://www.graalvm.org/reference-manual/wasm/ is for running wasm inside JVM, not for running Java projects within WebAssembly.

UPDATE 2025:

  1. webassembly.org list now has:
  1. With GraalVM 25 one can compile java to wasm.
    See @gouessej answer below
    and Compile Java to WebAssembly (with GraalVM)
    Thanks to @ozkanpakdil

Solution

  • Here are a few compilers that can do this for you:

    https://github.com/konsoletyper/teavm (most popular & my own reccomendation: https://teavm.org/)

    https://github.com/i-net-software/JWebAssembly (only to webassembly, the others can do webassembly and javascript)

    https://github.com/mirkosertic/Bytecoder

    https://github.com/leaningtech/cheerpj-meta

    Do note, that all of them have their limitations, most commonly that every Java class in Java's standard library won't work well with it (for example, TeaVM and others have problems with reflection).

    They also require pipeline integration -- make sure to be using a build tool these compilers support, such as gradle or maven