I am trying to compile Java to WebAssembly. Is this possible? My initial approach was to take the example described in this blog post
The approach described in the blog post is as follows:
I get to step 4 but after that I can't compile the LLVM IR bit code to wasm.
I get the following error from LLVM:
LLVM ERROR: unsupported GC: compressed-pointer
Has anyone tried this and succeeded?
With early access build of GraalVM 25 you can compile java to wasm which also requires binaryen, I downloaded windows package from https://github.com/WebAssembly/binaryen/releases and put it into Path. And for GraalVM I used https://github.com/graalvm/oracle-graalvm-ea-builds/releases
ozkan@HP-ENVY-2021-I7 C:\tmp
$ native-image --tool:svm-wasm HelloWasm
============================================================================================================
GraalVM Native Image: Generating 'hellowasm.exe' (executable)...
============================================================================================================
For detailed information and explanations on the build output, visit:
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
------------------------------------------------------------------------------------------------------------
[1/8] Initializing... (29.1s @ 0.15GB)
Java version: 25+14-LTS, vendor version: Oracle GraalVM 25-dev+14.1
Graal compiler: optimization level: 2, target machine: unknown, PGO: off
C compiler: cl.exe (microsoft, x64, 19.43.34809)
Garbage collector: JS-runtime-provided GC (max heap size: unknown)
1 user-specific feature(s):
- com.oracle.svm.thirdparty.gson.GsonFeature
------------------------------------------------------------------------------------------------------------
Build resources:
- 12.79GB of memory (75.6% of system memory, less than 8GB of memory available)
- 8 thread(s) (100.0% of 8 available processor(s), determined at start)
Warning: We could not find any version info for the following classes:
org.graalvm.aotjs.api.JSError
org.graalvm.aotjs.api.JSBoolean
org.graalvm.aotjs.api.JSNumber
org.graalvm.aotjs.api.JSUndefined
org.graalvm.aotjs.api.JSObject
org.graalvm.aotjs.api.JSSymbol
org.graalvm.aotjs.api.JSString
HelloWasm
org.graalvm.aotjs.api.JSValue
org.graalvm.aotjs.api.JSBigInt
[2/8] Performing analysis... [***] (12.1s @ 0.18GB)
1,018 reachable types (43.5% of 2,340 total)
745 reachable fields (29.4% of 2,534 total)
3,111 reachable methods (20.1% of 15,456 total)
408 types, 31 fields, and 111 methods registered for reflection
[3/8] Building universe... (2.2s @ 0.23GB)
[4/8] Parsing methods... [*] (1.0s @ 0.19GB)
[5/8] Inlining methods... [***] (0.7s @ 0.21GB)
[6/8] Compiling methods... [**] (3.0s @ 0.23GB)
[7/8] Laying out methods... [*] (0.3s @ 0.24GB)
[8/8] Creating image... [***] (9.3s @ 0.26GB)
0.00B ( 0.00%) for code area: 1,630 compilation units
672.71kB (60.25%) for image heap: 9,174 objects and 9 resources
443.90kB (39.75%) for other data
1.12MB in total
------------------------------------------------------------------------------------------------------------
Top 10 origins of code area: Top 10 object types in image heap:
0.00B org.graalvm.collections 130.88kB byte[] for java.lang.String
0.00B org.graalvm.extraimage.builder 130.30kB com.oracle.svm.core.hub.DynamicHubCompanion
0.00B com.oracle.svm.svm_enterprise 107.00kB java.lang.Class
0.00B svm.jar (Native Image) 70.09kB char[]
0.00B org.graalvm.webimage.api 66.82kB java.lang.String
0.00B jdk.graal.compiler 21.17kB c.o.s.c.hub.DynamicHub$ReflectionMetadata
0.00B jdk.proxy3 15.52kB byte[] for general heap data
0.00B jdk.proxy1 15.05kB byte[] for reflection metadata
0.00B jdk.proxy2 13.06kB c.o.s.c.configure.ConditionalRuntimeValue
0.00B java.base 12.35kB java.lang.String[]
0.00B for 1 more packages 90.48kB for 187 more object types
Use '--emit build-report' to create a report with more details.
------------------------------------------------------------------------------------------------------------
Security report:
- Binary includes Java deserialization.
- CycloneDX SBOM with 6 component(s) is embedded in binary (486.00B).
------------------------------------------------------------------------------------------------------------
Recommendations:
PGO: Use Profile-Guided Optimizations ('--pgo') for improved throughput.
HEAP: Set max heap for improved and more predictable memory usage.
CPU: Enable more CPU features with '-march=native' for improved performance.
QBM: Use the quick build mode ('-Ob') to speed up builds during development.
------------------------------------------------------------------------------------------------------------
2.1s (3.2% of total time) in 60 GCs | Peak RSS: 874.91MB | CPU load: 3.87
------------------------------------------------------------------------------------------------------------
Build artifacts:
C:\tmp\hellowasm.js (executable)
C:\tmp\hellowasm.js.wasm (executable)
C:\tmp\hellowasm.js.wat (build_info)
============================================================================================================
Finished generating 'hellowasm' in 59.9s.
ozkan@HP-ENVY-2021-I7 C:\tmp
$ node hellowasm.js
Özkan was here :) Hello from WebAssembly generated with GraalVM!
ozkan@HP-ENVY-2021-I7 C:\tmp
$ type HelloWasm.java
public class HelloWasm {
public static void main(String[] args) {
System.out.println("Özkan was here :) Hello from WebAssembly generated with GraalVM!");
}
}
ozkan@HP-ENVY-2021-I7 C:\tmp
$ dir
Volume in drive C is Windows
Volume Serial Number is BC27-F52E
Directory of C:\tmp
Sat 29/03/2025 08:24 <DIR> .
Sat 29/03/2025 08:34 477 HelloWasm.class
Sat 29/03/2025 08:34 188 HelloWasm.java
Sat 29/03/2025 08:35 97,261 hellowasm.js
Sat 29/03/2025 08:35 1,019,354 hellowasm.js.wasm
Sat 29/03/2025 08:35 25,440,609 hellowasm.js.wat
5 File(s) 26,557,889 bytes
1 Dir(s) 91,167,571,968 bytes free
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With