Packaging a Java application as WASM

151 views
Skip to first unread message

Arun Gupta

unread,
Jun 24, 2023, 2:20:56 AM6/24/23
to TeaVM
Hello there,

I'm trying to create a simple sample that will take a Java application and package it as a WASM application. Then I'd like to package it as a Docker container and eventually deploy it in my Kubernetes cluster.

What does a WASM file structure look like? This will allow me to update my pom.xml and package the application accordingly.

Here is my maven file:

<?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>org.sample</groupId>
<artifactId>java-wasm-hello</artifactId>
<version>1.0-SNAPSHOT</version>

<name>java-wasm-hello</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<java.version>11</java.version>
<teavm.version>0.8.1</teavm.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<!-- Emulator of Java class library for TeaVM -->
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-classlib</artifactId>
<version>${teavm.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Configure Java compiler to use Java 8 syntax -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>

<plugin>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
<version>${teavm.version}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<mainClass>org.sample.App</mainClass>
<debugInformationGenerated>true</debugInformationGenerated>
<sourceMapsGenerated>true</sourceMapsGenerated>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Thanks!

Alexey Andreev

unread,
Jun 24, 2023, 2:22:45 AM6/24/23
to TeaVM
Hello. Sorry, I don't understand your question. What do you want to know?

суббота, 24 июня 2023 г. в 08:20:56 UTC+2, arun....@gmail.com:

Arun Gupta

unread,
Jun 24, 2023, 1:41:42 PM6/24/23
to TeaVM
I am a total newbie to Web Assembly so trying to understand the flow.

I'm trying to understand how to use WebAssembly in a containerized world. For that, I have a simple Hello World Java application. I'd like to understand how to package this application as Web Assembly.

All the examples I've seen are targeted to run in a browser. How do I get it running in a non-browser environment? Or is that even not possible? Am I thinking straight?

Thanks for your patience.

Alexey Andreev

unread,
Jun 24, 2023, 3:03:28 PM6/24/23
to TeaVM
I have not idea how to use Wasm in containerized world. However, if you want to generate WASI runtime compatible Wasm file, you can look at example here (uses Gradle). Some documentation is available for Gradle here. You are right, for Maven there's no documentation, but remember that code is documentation, so according to this file, you can find list of available targets in this enum. What you need is WEBASSEMBLY_WASI target type.

But what problem are you trying to solve by this strange kind of packaging? Why not just run Java application within normal JVM? If you want it containerized, why don't you containerize JRE?
суббота, 24 июня 2023 г. в 19:41:42 UTC+2, arun....@gmail.com:

marian cekan

unread,
Aug 31, 2023, 8:58:30 AM8/31/23
to TeaVM
Hi there, I guess I have a similar problem did you manage to solve your question please?

Dátum: sobota 24. júna 2023, čas: 21:03:28 UTC+2, odosielateľ: Alexey Andreev
Reply all
Reply to author
Forward
0 new messages