gpt4 book ai didi

java - TeaVM 和 WebAssembly - 构建错误

转载 作者:行者123 更新时间:2023-12-02 02:17:04 28 4
gpt4 key购买 nike

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

首先,我使用以下命令创建了一个原型(prototype):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 文件):

error when 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 而不会出现错误?预先感谢您!

最佳答案

TeaVM 的 Wasm 后端不支持 JSO 互操作层。它还支持 JavaScript 后端可用的功能子集。所以没有办法让 TeaVM Flavor 在 Wasm 中工作,相反你应该更喜欢 JavaScript 目标。如果你想了解如何应对 Wasm BE,可以看看example .

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

关于java - TeaVM 和 WebAssembly - 构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67016039/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com