Jan*_*ine 2 java webassembly teavm teavm-flavour
我想在网络中使用 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/web assembly- 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>
Run Code Online (Sandbox Code Playgroud)
并在插件部分添加以下内容:
<targetType>WEBASSEMBLY</targetType>
<optimizationLevel>FULL</optimizationLevel>
<heapSize>8</heapSize>
Run Code Online (Sandbox Code Playgroud)
我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我执行 mvn clean package 时,出现以下错误(但创建了 wasm 文件):
我的完整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>
Run Code Online (Sandbox Code Playgroud)
如何创建完整的 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 的通知。
| 归档时间: |
|
| 查看次数: |
1550 次 |
| 最近记录: |