40

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

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


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

Paul Verest
  • 60,022
  • 51
  • 208
  • 332

1 Answers1

11

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

LeoDog896
  • 3,472
  • 1
  • 15
  • 40
  • 1
    AFAIK, all of the projects linked above target web browser WASM runtime, i.e. no WASI and you are expected to write Javascript code to interacti with the WASM module (or have it generated for you, in the case of chherpj). – Guss Oct 25 '22 at 10:42