TeaVM and WebAssembly - build errors

1.6k Views Asked by At

I want to use Java code in the web. For this I want to convert Java to WASM and use this wasm-file in JavaScript. For converting Java to WebAssembly, I am using TeaVM.

First, I created an archetype with this command: mvn archetype:generate -DarchetypeGroupId=org.teavm.flavour -DarchetypeArtifactId=teavm-flavour-application -DarchetypeVersion=0.2.0

In addition, I added these two dependencies (according to http://blog.dmitryalexandrov.net/webassembly-for-java-developers/):

   <dependency>
        <groupId>org.teavm</groupId>
        <artifactId>teavm-jso-apis</artifactId>
        <version>${teavm.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.teavm</groupId>
        <artifactId>teavm-interop</artifactId>
        <version>${teavm.version}</version>
    </dependency>

and added the following in the plugin section:

<targetType>WEBASSEMBLY</targetType>
<optimizationLevel>FULL</optimizationLevel>
<heapSize>8</heapSize>

My Java file:

@BindTemplate("templates/client.html")
public class Client extends ApplicationTemplate {
    private String userName = "ABC";

    public static void main(String[] args) {
        Client client = new Client();
        client.bind("application-content");
    }
    @Export(name = "getUserName")
    public String getUserName() {
        return userName;
    }
}

But when I am doing mvn clean package, I am getting to following error (but a wasm file is created):

error when mvn clean package

my complete pom:

<?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>my.company</groupId>
  <artifactId>java_wasm</artifactId>
  <version>1.0-SNAPSHOT</version>

  <packaging>war</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <flavour.version>0.2.0</flavour.version>
    <teavm.version>0.6.0</teavm.version>
    <jackson.version>2.5.4</jackson.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.teavm</groupId>
      <artifactId>teavm-classlib</artifactId>
      <version>${teavm.version}</version>
    </dependency>
    <dependency>
      <groupId>org.teavm</groupId>
      <artifactId>teavm-metaprogramming-impl</artifactId>
      <version>${teavm.version}</version>
    </dependency>

    <dependency>
      <groupId>org.teavm.flavour</groupId>
      <artifactId>teavm-flavour-widgets</artifactId>
      <version>${flavour.version}</version>
    </dependency>
    <dependency>
      <groupId>org.teavm.flavour</groupId>
      <artifactId>teavm-flavour-rest</artifactId>
      <version>${flavour.version}</version>
    </dependency>
    <dependency>
        <groupId>org.teavm</groupId>
        <artifactId>teavm-jso-apis</artifactId>
        <version>${teavm.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.teavm</groupId>
        <artifactId>teavm-interop</artifactId>
        <version>${teavm.version}</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${jackson.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>

      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <webResources>
            <resource>
              <directory>${project.build.directory}/generated/js</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.teavm</groupId>
        <artifactId>teavm-maven-plugin</artifactId>
        <version>${teavm.version}</version>
        <executions>
          <execution>
            <id>web-client</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <targetDirectory>${project.build.directory}/generated/js/teavm</targetDirectory>
              <mainClass>my.company.Client</mainClass>
              <minifying>true</minifying>
              <debugInformationGenerated>true</debugInformationGenerated>
              <sourceMapsGenerated>true</sourceMapsGenerated>
              <sourceFilesCopied>true</sourceFilesCopied>
              <optimizationLevel>ADVANCED</optimizationLevel>
              <targetType>WEBASSEMBLY</targetType>
              <optimizationLevel>FULL</optimizationLevel>
              <heapSize>8</heapSize>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

How can I create a complete WASM without errors? Thank you in advance!

1

There are 1 best solutions below

2
Alexey Andreev On BEST ANSWER

Wasm backend of TeaVM does not support JSO interop layer. It also supports subset of features available in JavaScript backend. So there's no way to make TeaVM Flavour work in Wasm, instead your should prefer JavaScript target. If you want to learn how to deal with Wasm BE, you can take a look at example.

Wasm has proven to be extremely inappropriate to run Java, so I recommend to use JavaScript BE of TeaVM. Also, please note that official site (htts://teavm.org) lists links where you can get help (google groups, gitter, direct email). I don't follow StackOverflow questions about TeaVM and don't receive notifications from SO.