How to compile Java to WASM (WebAssembly)?
Asked Answered
L

2

47

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.

Lapides answered 6/7, 2021 at 6:55 Comment(4)
Kotlin is listed there.Berget
github.com/i-net-software/JWebAssembly might helpBerget
That is interesting. I may give a try. It is somehow out of scope of original answer as JWebAssembly is taking .class files not Java, but is good answer if giving some hints how to integrate into dev pipeline.Lapides
I also found several compilers that translate Java bytecode to WebAssembly.Dionisio
B
15

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

Bamberger answered 8/10, 2022 at 0:38 Comment(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).Hammered
G
3

Running Java bytecode inside of a Wasm runtime is possible a few ways. One is to use TeaVM. This page tracks the progress of various methods: https://www.fermyon.com/wasm-languages/java

Goffer answered 26/10, 2023 at 22:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.