You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TeaVM编译Tnoodle Java库为WASM时遇空指针错误求助

问题:使用TeaVM将Tnoodle Java库编译为WASM时出现空指针错误

我尝试引入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>

Maven构建日志

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\repository\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\repository\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:\Users\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.jar: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

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>
        </plugins>
    </build>
</project>

内容的提问来源于stack exchange,提问作者nsvot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 20:52:57