1

我想在网络中使用 Java 代码。为此,我想将 Java 转换为 WASM 并在 JavaScript 中使用这个 wasm 文件。为了将 Java 转换为 WebAssembly,我使​​用的是 TeaVM。

首先,我用这个命令创建了一个原型:mvn archetype:generate -DarchetypeGroupId=org.teavm.flavour -DarchetypeArtifactId=teavm-flavour-application -DarchetypeVersion=0.2.0

另外,我添加了这两个依赖项(根据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>

并在插件部分添加以下内容:

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

我的 Java 文件:

@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;
    }
}

但是当我在做 mvn clean package 时,我遇到了以下错误(但创建了一个 wasm 文件):

mvn clean package时出错

我完整的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>

如何创建完整的 WASM 而不会出错?先感谢您!

4

1 回答 1

4

TeaVM 的 Wasm 后端不支持 JSO 互操作层。它还支持 JavaScript 后端中可用的功能子集。所以没有办法让 TeaVM Flavor 在 Wasm 中工作,而是你应该更喜欢 JavaScript 目标。如果你想学习如何处理 Wasm BE,可以看一下example

Wasm 已被证明非常不适合运行 Java,因此我建议使用 TeaVM 的 JavaScript BE。另外,请注意官方网站 (htts://teavm.org) 列出了您可以获得帮助的链接(谷歌群组、gitter、直接电子邮件)。我没有关注有关 TeaVM 的 StackOverflow 问题,也没有收到来自 SO 的通知。

于 2021-04-19T08:09:23.843 回答