Native methods cannot be compiled to WebAssembly using JWebAssembly

138 Views Asked by At

I have this class which I want to compile to wasm.

package org.example;

import de.inetsoftware.jwebassembly.api.annotation.Export;
import de.inetsoftware.jwebassembly.api.annotation.Replace;
import de.inetsoftware.jwebassembly.web.dom.Document;
import de.inetsoftware.jwebassembly.web.dom.HTMLElement;
import de.inetsoftware.jwebassembly.web.dom.Text;
import de.inetsoftware.jwebassembly.web.dom.Window;

public class Main {

    @Export
    public static void main() {
        Document document = Window.document();
        HTMLElement div = document.createElement("div");
        Text text = document.createTextNode("Hello World, this text come from WebAssembly.");
        div.appendChild( text );
        document.body().appendChild( div );
    }
   
}

And this is my build.gradle

buildscript {
    repositories {
        maven { url uri('https://jitpack.io') }
    }
    dependencies {
        classpath 'com.github.i-net-software:jwebassembly-gradle:master-SNAPSHOT'
    }
}


plugins {
    id 'java'
}
apply plugin: 'de.inetsoftware.jwebassembly'
wasm {
    format = 'Text'
    compilerVersion = 'com.github.i-net-software:jwebassembly:master-SNAPSHOT'

    //Write method and parameter names into the output *.wasm file.
    //The file will be approximate 10% larger. And it generate a source map.
    //With a source map you can see in the debugger of the browser your source code if available.
    debugNames = true // configuration options for the wasm task go here
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

dependencies {
    compileOnly 'com.github.i-net-software:jwebassembly-api:master-SNAPSHOT'
    implementation 'com.github.i-net-software:jwebassembly-api:master-SNAPSHOT'
    implementation 'de.inetsoftware:jwebassembly-api:+'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
    useJUnitPlatform()
}

archivesBaseName = 'FirstWasm'

sourceSets {
    main {
        java {
            srcDir 'src/main/java/org.example/'
        }
        resources {
            srcDir 'src/main/resources'
        }
    }
}

apply plugin:'application'
mainClassName = "org.example.Main"

When I want to compile to wasm I get this error: Native methods cannot be compiled to WebAssembly: java/lang/ref/Reference.clear0()V If you want to use classes with native code, you must use a library that implements these native methods, such as 'de.inetsoftware:jwebassembly-api:+' or implements this native method self. at java.lang.ref.Reference.clear0(Reference.java)

Does anybody know how to fix it? As you can see I use the library de.inetsoftware:jwebassembly-api:+.

1

There are 1 best solutions below

0
Horcrux7 On

JWebAssembly compile currently only with Java 8 runtime. Java 11 and higher is in progress. There is a large break vorm Java 8 to Java 9 through the module system.