IK.AM

@making's tech note


Generate WebAssembly in Java

πŸ—ƒ {Programming/WebAssembly/Java}
🏷 Java 🏷 TeaVM 🏷 WebAssembly 
πŸ—“ Updated at 2022-12-26T00:59:37ZΒ Β πŸ—“ Created at 2022-12-25T05:47:13ZΒ {βœ’οΈοΈΒ EditΒ  ⏰ HistoryΒ  πŸ—‘Β Delete}Β  πŸ‡―πŸ‡΅Β Original entry

I read the following articles about Java's support for WebAssembly, and both TeaVM's Fermyon fork version (WASI supported) was introduced as promising, so I tried it.

Prepare pom.xml like below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>hello-wasm</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <teavm.version>0.2.7</teavm.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.fermyon</groupId>
            <artifactId>teavm-classlib</artifactId>
            <version>${teavm.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
            </plugin>
            <plugin>
                <groupId>com.fermyon</groupId>
                <artifactId>teavm-maven-plugin</artifactId>
                <version>${teavm.version}</version>
                <executions>
                    <execution>
                        <id>web-client</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <targetDirectory>
                                ${project.build.directory}/generated/wasm/teavm-wasm
                            </targetDirectory>
                            <targetType>WEBASSEMBLY</targetType>
                            <mainClass>com.example.Main</mainClass>
                            <debugInformationGenerated>true</debugInformationGenerated>
                            <minifying>false</minifying>
                            <optimizationLevel>SIMPLE</optimizationLevel>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Prepare Hello World Java code

package com.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

Compile via Maven Plugin

./mvnw clean package

It will generate files like bellow:

$ tree target/generated
target/generated
└── wasm
    └── teavm-wasm
        β”œβ”€β”€ classes.wasm
        β”œβ”€β”€ classes.wasm-runtime.js
        β”œβ”€β”€ classes.wasm.c
        β”œβ”€β”€ classes.wast
        └── node_modules
            └── @bjorn3
                └── browser_wasi_shim
                    └── src
                        β”œβ”€β”€ fd.js
                        β”œβ”€β”€ fs_core.js
                        β”œβ”€β”€ fs_fd.js
                        β”œβ”€β”€ wasi.js
                        └── wasi_defs.js

6 directories, 9 files

Run with wasmtime

$ wasmtime target/generated/wasm/teavm-wasm/classes.wasm
Hello world!

Worked!

If you look at this sample you can see what you can do.

The source code I tried this time is https://github.com/making/hello-wasm-java


Β Β Β