Got the error Failed to execute goal org.teavm:teavm-maven-plugin:0.5.1, error occured: Cannot invoke "org.teavm.model.MethodReader.getAnnotations()

huangapple go评论68阅读模式
英文:

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&#39;t get any.
I don&#39;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 = &quot;getScramble&quot;)
public static void getScramble(String puzzleType) {
Puzzle puzzle = new ThreeByThreeCubePuzzle();
String scramble = puzzle.generateScramble();
setScramble(scramble);
}
@Import(module = &quot;env&quot;, name = &quot;setScramble&quot;)
private static native void setScramble(String scramble);
}

This is the example code for the browser:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Scrambler&lt;/title&gt;
    &lt;style&gt;
      section.container {
        margin: 3em;
      }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;script&gt;
      function setScramble(scramble) {
        console.log(&quot;WASM code has called the setScramble method.&quot;);
        console.log(&quot;WASM set the scramble &quot; + scramble);
      }
      WebAssembly.instantiateStreaming(fetch(&quot;./scramblelogic.wasm&quot;), {
        env: { setScramble },
      }).then((module) =&gt; {
        console.log(&quot;Calling the wasm module from JavaScript.&quot;);
        module.instance.exports.getScramble(&quot;333&quot;);
      });
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

This is the log:

PS C:\Users\njp28\Workspace\Projects\UpcubeScrambler&gt; 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 &#39;UTF-8&#39; 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&gt; 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 &#39;UTF-8&#39; 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 &quot;org.teavm.model.MethodReader.getAnnotations()&quot; because &quot;method&quot; is null -&gt; [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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.cubelelo&lt;/groupId&gt;
    &lt;artifactId&gt;UpcubeScrambler&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;properties&gt;
        &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.worldcubeassociation.tnoodle&lt;/groupId&gt;
            &lt;artifactId&gt;lib-scrambles&lt;/artifactId&gt;
            &lt;version&gt;0.18.0&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.teavm&lt;/groupId&gt;
            &lt;artifactId&gt;teavm-classlib&lt;/artifactId&gt;
            &lt;version&gt;0.5.1&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;1.8&lt;/source&gt;
                    &lt;target&gt;1.8&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.4&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;webResources&gt;
                        &lt;resource&gt;
                        &lt;directory&gt;${project.build.directory}/webapp&lt;/directory&gt;
                        &lt;/resource&gt;
                    &lt;/webResources&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                &lt;version&gt;1.6.0&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;java&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
                        &lt;includePluginDependencies&gt;true&lt;/includePluginDependencies&gt;
                    &lt;mainClass&gt;fi.iki.elonen.SimpleWebServer&lt;/mainClass&gt;
                    &lt;arguments&gt;
                        &lt;argument&gt;-d&lt;/argument&gt;
                        &lt;argument&gt;${project.build.directory}/${project.build.finalName}&lt;/argument&gt;
                    &lt;/arguments&gt;
                &lt;/configuration&gt;
                &lt;dependencies&gt;
                    &lt;dependency&gt;
                        &lt;groupId&gt;org.nanohttpd&lt;/groupId&gt;
                        &lt;artifactId&gt;nanohttpd-webserver&lt;/artifactId&gt;
                        &lt;version&gt;2.3.1&lt;/version&gt;
                    &lt;/dependency&gt;
                &lt;/dependencies&gt;
            &lt;/plugin&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.teavm&lt;/groupId&gt;
            &lt;artifactId&gt;teavm-maven-plugin&lt;/artifactId&gt;
            &lt;version&gt;0.5.1&lt;/version&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;id&gt;hello&lt;/id&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;compile&lt;/goal&gt;
                    &lt;/goals&gt;
                    &lt;configuration&gt;
                        &lt;mainClass&gt;com.cubelelo.ScrambleLogic&lt;/mainClass&gt;
                        &lt;targetDirectory&gt;${project.build.directory}/webapp/wasm&lt;/targetDirectory&gt;
                        &lt;targetFileName&gt;scramblelogic.wasm&lt;/targetFileName&gt;
                        &lt;targetType&gt;WEBASSEMBLY&lt;/targetType&gt;
                        &lt;optimizationLevel&gt;FULL&lt;/optimizationLevel&gt;
                    &lt;/configuration&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

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

  1. 您正在使用相当旧的TeaVM版本,请使用最新版本(如果可能的话,请使用预览版本)。
  2. 我建议从JavaScript目标开始,而不是WebAssembly。TeaVM中的WebAssembly支持是实验性的,存在一些处理困难。我建议先编译到JavaScript,如果一切顺利,再尝试“升级”到WebAssembly。
  3. TeaVM从未设计用于移植库,因此您应该始终拥有“main”方法,并在WebAssembly的情况下始终首先调用“main”方法,以便TeaVM有机会正确初始化虚拟机。如果成功编译了默认入口点,请与我联系,以便我帮助您创建替代入口点。
  4. 如果您能分享完整的配置,包括源代码和简单可重现的步骤列表,以帮助重现问题,那将非常好。

我建议使用问题跟踪器进行进一步的沟通。

更新:我添加了自己的示例。唯一的问题是缺少SecureRandom类,我创建了一个简单的替代品。请参阅https://github.com/konsoletyper/tnoodle-example。

英文:
  1. You are using quite old version of TeaVM, please use most recent version (if possible, use preview build).
  2. 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.
  3. 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.
  4. 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

huangapple
  • 本文由 发表于 2023年5月25日 16:40:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定