---
title: JavaでWebAssemblyを生成してみる
tags: ["Java", "WebAssembly", "TeaVM"]
categories: ["Programming", "WebAssembly", "Java"]
date: 2022-12-25T05:42:49Z
updated: 2022-12-26T00:59:27Z
---

JavaのWebAssembly対応に関する以下の記事を読んだところ、どちらも[TeaVMのFermyon fork版](https://github.com/fermyon/teavm-wasi) (WASI対応) が有望であると紹介していたので試してみた。

* https://seb.deleuze.fr/posts/webassembly-impact-on-programming-languages/
* https://www.javaadvent.com/2022/12/webassembly-for-the-java-geek.html


以下のようなpom.xmlを用意する

```xml
<?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>
```

Hello WorldなJavaコードを用意

```java
package com.example;

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

Maven Plugin経由でコンパイル

```
./mvnw clean package
```

次のようなファイルが生成される

```
$ 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
```

[wasmtime](https://wasmtime.dev/)で実行


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

動いた！


この[サンプル](https://github.com/fermyon/teavm-wasi/blob/master/tests/wasi/src/main/java/wasi/Test.java)を見るといろいろできそう。


今回試したソースコードは https://github.com/making/hello-wasm-java
