英文:
Got the error Failed to execute goal org.teavm:teavm-maven-plugin:0.5.1, error occured: Cannot invoke "org.teavm.model.MethodReader.getAnnotations()
问题
抱歉,我只能为您提供翻译,不提供其他内容。以下是您的翻译内容:
我尝试在Java库中包含`org.worldcubeassociation.tnoodle.scrambles.Puzzle`,并尝试调用抽象类Puzzle的方法。在构建wasm时,我遇到了错误。我得到的错误消息是:
无法执行目标org.teavm:teavm-maven-plugin:0.5.1:compile (hello)于项目UpcubeScrambler: 发生意外错误:无法调用"org.teavm.model.MethodReader.getAnnotations()",因为"method"为空 -> [帮助 1]
我尝试寻找解决方案,但未找到任何解决方法。
我不常使用Java。我需要在前端自身实现此功能,但Tnoodle库仅在Java中可用。
请帮助我解决这个问题。
这是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代码调用了setScramble方法。");
console.log("WASM设置了scramble " + scramble);
}
WebAssembly.instantiateStreaming(fetch("./scramblelogic.wasm"), {
env: { setScramble },
}).then((module) => {
console.log("从JavaScript调用wasm模块。");
module.instance.exports.getScramble("333");
});
</script>
</body>
</html>
这是日志:
PS C:\Users\njp28\Workspace\Projects\UpcubeScrambler> mvn package
[INFO] Scanning for projects...
...
[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]
...
这是pom.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>
</
<details>
<summary>英文:</summary>
I tried to include a Java library `org.worldcubeassociation.tnoodle.scrambles.Puzzle` and I tried to invoke a method of the abstract class Puzzle. I got an error while building the wasm. The error I am getting is:
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]
I tried to search for a solution but couldn't get any.
I don't work with Java much. I need to implement this functionality in the front end itself, but the Tnoodle(library) is only available in java.
Please help me here.
This is the Java code:
```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);
}
This is the example code for the browser:
<!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>
This is the log:
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
This is the pop.xml file:
<?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>
I have used the above code to run the Java code in the JavaScript.
I got the error while running the code.
Please help me solve this issue.
答案1
得分: 0
- 您正在使用相当旧的TeaVM版本,请使用最新版本(如果可能的话,请使用预览版本)。
- 我建议从JavaScript目标开始,而不是WebAssembly。TeaVM中的WebAssembly支持是实验性的,存在一些处理困难。我建议先编译到JavaScript,如果一切顺利,再尝试“升级”到WebAssembly。
- TeaVM从未设计用于移植库,因此您应该始终拥有“main”方法,并在WebAssembly的情况下始终首先调用“main”方法,以便TeaVM有机会正确初始化虚拟机。如果成功编译了默认入口点,请与我联系,以便我帮助您创建替代入口点。
- 如果您能分享完整的配置,包括源代码和简单可重现的步骤列表,以帮助重现问题,那将非常好。
我建议使用问题跟踪器进行进一步的沟通。
更新:我添加了自己的示例。唯一的问题是缺少SecureRandom类,我创建了一个简单的替代品。请参阅https://github.com/konsoletyper/tnoodle-example。
英文:
- You are using quite old version of TeaVM, please use most recent version (if possible, use preview build).
- I consider starting from JavaScript target instead of WebAssembly. WebAssembly support is experimental in TeaVM and has some difficulties to deal with. I would recommend to compile to JavaScript and if everything goes fine, try to "upgrade" to WebAssembly.
- TeaVM was never designed to port libraries, so you should always have "main" method, and in case of WebAssembly you should always call "main" first, to give TeaVM a chance to properly initialize VM. If you get success with compiling default entry point, please, contact me so that I helped you to create alternative entry points.
- It would be great if you could share full configuration, including source code and simple reproducible list of steps that help to reproduce the issue.
I consider using issue tracker for further communication.
UPDATE: added my own example. The only issue was absence of SecureRandom class, for which I created a simple replacement. See https://github.com/konsoletyper/tnoodle-example
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论