我尝试包含一个Java库org.worldcubeassociation.tnoodle.scrambles.Puzzle
,并尝试调用抽象类Puzzle的方法。我在构建wasm时遇到了一个错误。我得到的错误是:
Failed to execute goal org.teavm:teavm-maven-plugin:0.5.1:compile (hello) on project UpcubeScrambler: Unexpected error occured: Cannot invoke "org.teavm.model.MethodReader.getAnnotations()" because "method" is null -> [Help 1]
我试图寻找解决办法,但没有找到。
我不怎么用Java。我需要在前端本身实现此功能,但Tnoodle(库)仅在Java中可用。请帮帮我。
下面是Java代码:
package com.cubelelo;
import org.teavm.interop.Export;
import org.teavm.interop.Import;
import org.worldcubeassociation.tnoodle.puzzle.*;
import org.worldcubeassociation.tnoodle.scrambles.Puzzle;
public class ScrambleLogic {
@Export(name = "getScramble")
public static void getScramble(String puzzleType) {
Puzzle puzzle = new ThreeByThreeCubePuzzle();
String scramble = puzzle.generateScramble();
setScramble(scramble);
}
@Import(module = "env", name = "setScramble")
private static native void setScramble(String scramble);
}
这是浏览器的示例代码:
<!DOCTYPE html>
<html>
<head>
<title>Scrambler</title>
<style>
section.container {
margin: 3em;
}
</style>
</head>
<body>
<script>
function setScramble(scramble) {
console.log("WASM code has called the setScramble method.");
console.log("WASM set the scramble " + scramble);
}
WebAssembly.instantiateStreaming(fetch("./scramblelogic.wasm"), {
env: { setScramble },
}).then((module) => {
console.log("Calling the wasm module from JavaScript.");
module.instance.exports.getScramble("333");
});
</script>
</body>
</html>
这是日志:
PS C:\Users\njp28\Workspace\Projects\UpcubeScrambler> mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building UpcubeScrambler 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ UpcubeScrambler ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ UpcubeScrambler ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\Users\njp28\Workspace\Projects\UpcubeScrambler\target\classes
[INFO]
[INFO] --- teavm-maven-plugin:0.5.1:compile (hello) @ UpcubeScrambler ---
[INFO] Preparing classpath for JavaScript generation
[INFO] Using the following classpath for JavaScript generation: C:\Users\njp28\.m2\repository\org\worldcubeassociation\tnoodle\lib-scrambles\0.18.
0\lib-scrambles-0.18.0.jar:C:\Users\njp28\.m2\repository\org\worldcubeassociation\tnoodle\lib-svglite\0.18.0\lib-svglite-0.18.0.jar:C:\Users\njp28
\.m2\repository\org\timepedia\exporter\gwtexporter\2.5.1\gwtexporter-2.5.1.jar:C:\Users\njp28\.m2\repository\org\teavm\teavm-classlib\0.5.1\teavm-
classlib-0.5.1.jar:C:\Users\njp28\.m2\repository\org\teavm\teavm-platform\0.5.1\teavm-platform-0.5.1.jar:C:\Users\njp28\.m2\repository\org\teavm\t
eavm-core\0.5.1\teavm-core-0.5.1.jar:C:\Users\njp28\.m2\repository\org\teavm\teavm-interop\0.5.1\teavm-interop-0.5.1.jar:C:\Users\njp28\.m2\reposi
tory\commons-io\commons-io\2.4\commons-io-2.4.jar:C:\Users\njp28\.m2\repository\com\carrotsearch\hppc\0.6.1\hppc-0.6.1.jar:C:\Users\njp28\.m2\repo
PS C:\Users\njp28\Workspace\Projects\UpcubeScrambler> mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building UpcubeScrambler 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ UpcubeScrambler ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ UpcubeScrambler ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- teavm-maven-plugin:0.5.1:compile (hello) @ UpcubeScrambler ---
[INFO] Preparing classpath for JavaScript generation
[INFO] Using the following classpath for JavaScript generation: C:\Users\njp28\.m2\repository\org\worldcubeassociation\tnoodle\lib-scrambles\0.18.0\lib-scrambles-0.18.0.jar:C:\Users\njp28\.m2\repository\org\worldcubeassociation\tnoodle\lib-svglite\0.18.0\lib-svglite-0.18.0.jar:C:\Users\njp28\.m2\repository\org\timepedia\exporter\gwtexporter\2.5.1\gwtexporter-2.5.1.jar:C:\Users\njp28\.m2\repository\org\teavm\teavm-classlib\0.5.1\teavm-classlib-0.5.1.jar:C:\Users\njp28\.m2\repository\org\teavm\teavm-platform\0.5.1\teavm-platform-0.5.1.jar:C:\Users\njp28\.m2\repository\org\teavm\teavm-core\0.5.1\teavm-core-0.5.1.jar:C:\Users\njp28\.m2\repository\org\teavm\teavm-interop\0.5.1\teavm-interop-0.5.1.jar:C:\Users\njp28\.m2\reposi
tory\commons-io\commons-io\2.4\commons-io-2.4.jar:C:\Users\njp28\.m2\repository\com\carrotsearch\hppc\0.6.1\hppc-0.6.1.jar:C:\Users\njp28\.m2\repo
sitory\org\teavm\teavm-jso\0.5.1\teavm-jso-0.5.1.jar:C:\Users\njp28\.m2\repository\org\teavm\teavm-jso-apis\0.5.1\teavm-jso-apis-0.5.1.jar:C:\User
s\njp28\.m2\repository\org\teavm\teavm-jso-impl\0.5.1\teavm-jso-impl-0.5.1.jar:C:\Users\njp28\.m2\repository\org\mozilla\rhino\1.7.7\rhino-1.7.7.j
ar:C:\Users\njp28\.m2\repository\org\ow2\asm\asm-debug-all\5.2\asm-debug-all-5.2.jar:C:\Users\njp28\.m2\repository\com\google\code\gson\gson\2.2.4
\gson-2.2.4.jar:C:\Users\njp28\.m2\repository\com\jcraft\jzlib\1.1.3\jzlib-1.1.3.jar:C:\Users\njp28\.m2\repository\joda-time\joda-time\2.7\joda-time-2.7.jar:C:\Users\njp28\Workspace\Projects\UpcubeScrambler\target\classes
[INFO] Building JavaScript file
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.620s
[INFO] Finished at: Thu May 25 12:43:29 IST 2023
[INFO] Final Memory: 12M/54M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.teavm:teavm-maven-plugin:0.5.1:compile (hello) on project UpcubeScrambler: Unexpected error occured: Cannot invoke "org.teavm.model.MethodReader.getAnnotations()" because "method" is null -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
这是pop.xml文件:
<?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>com.cubelelo</groupId>
<artifactId>UpcubeScrambler</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.worldcubeassociation.tnoodle</groupId>
<artifactId>lib-scrambles</artifactId>
<version>0.18.0</version>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-classlib</artifactId>
<version>0.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>fi.iki.elonen.SimpleWebServer</mainClass>
<arguments>
<argument>-d</argument>
<argument>${project.build.directory}/${project.build.finalName}</argument>
</arguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.nanohttpd</groupId>
<artifactId>nanohttpd-webserver</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
<version>0.5.1</version>
<executions>
<execution>
<id>hello</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<mainClass>com.cubelelo.ScrambleLogic</mainClass>
<targetDirectory>${project.build.directory}/webapp/wasm</targetDirectory>
<targetFileName>scramblelogic.wasm</targetFileName>
<targetType>WEBASSEMBLY</targetType>
<optimizationLevel>FULL</optimizationLevel>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我使用上面的代码在JavaScript中运行Java代码。
我在运行代码时遇到了错误。请帮我解决这个问题。
1条答案
按热度按时间xzv2uavs1#
1.您使用的是相当旧的TeaVM版本,请使用最新版本(如果可能,请使用preview build)。
1.我考虑从JavaScript目标而不是WebAssembly开始。WebAssembly支持在TeaVM中是实验性的,处理起来有一些困难。我建议编译成JavaScript,如果一切顺利,尝试“升级”到WebAssembly。
1.如果您能分享完整的配置,包括源代码和简单的可重复的步骤列表,这将是伟大的,有助于重现问题。
我考虑使用issue tracker进行进一步的通信。
更新:添加了自己的示例。唯一的问题是没有SecureRandom类,我创建了一个简单的替代品。参见https://github.com/konsoletyper/tnoodle-example