I'm trying to figure out in the most rudimentary way possible how to use TeaVM to compile a wasm file from a .java file. My .java code is fairly simple and compiles successfully to Javascript if I use the maven .pom file provided on the TeaVM Getting Started page:
package org.teavm;
public class Scorer {
public static void main(String[] args) {
Double score = testScore();
//log(score);
}
//@JSBody(params = { "value" }, script = "console.log('Value is: ' + value)")
//public static native void log(double value);
public static double testScore(){
Double score = 8.0085;
return score;
}
}
My problem comes when I want to do the same thing, but compile it to .wasm instead of .js. (This is why the JSO stuff is commented out from the .java code). My .pom file is as follows:
<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>org.teavm</groupId>
<artifactId>teavm-maven-webapp</artifactId>
<version>0.6.1 </version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<teavm.version>0.6.1</teavm.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Emulator of Java class library for TeaVM -->
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-classlib</artifactId>
<version>${teavm.version}</version>
<scope>provided</scope>
</dependency>
<!-- JavaScriptObjects (JSO) - a JavaScript binding for TeaVM -->
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-jso-apis</artifactId>
<version>${teavm.version}</version>
<scope>provided</scope>
</dependency>
<!-- Servlet 3.1 specification -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Configure Java compiler to use Java 8 syntax -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Configure WAR plugin to include JavaScript !AND WASM! files generated by TeaVM -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>${project.build.directory}/generated/js</directory>
</resource>
<resource>
<directory>${project.build.directory}/generated/wasm</directory>
</resource>
</webResources>
</configuration>
</plugin>
<!-- Configure TeaVM -->
<plugin>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
<version>${teavm.version}</version>
<executions>
<execution>
<id>web-client</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<!-- Directory where TeaVM should put generated files. This configuration conforms to the settings
of the WAR plugin -->
<targetDirectory>${project.build.directory}/generated/js/teavm</targetDirectory>
<!-- Main class, containing static void main(String[]) -->
<mainClass>org.teavm.Scorer</mainClass>
<!-- Whether TeaVM should produce minified JavaScript. Can reduce JavaScript file size more than
two times -->
<minifying>false</minifying>
<!-- Whether TeaVM should produce debug information for its built-in debugger -->
<debugInformationGenerated>true</debugInformationGenerated>
<!-- Whether TeaVM should produce source maps file -->
<sourceMapsGenerated>true</sourceMapsGenerated>
<!-- Whether TeaVM should also put source files into output directory,
for compatibility with source maps -->
<sourceFilesCopied>true</sourceFilesCopied>
<!-- Optimization level. Valid values are: SIMPLE, ADVANCED, FULL -->
<optimizationLevel>FULL</optimizationLevel>
<!-- <targetType>WEBASSEMBLY</targetType> -->
</configuration>
</execution>
<execution>
<id>wasm-client</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<!-- Directory where TeaVM should put generated files. This configuration conforms to the settings
of the WAR plugin -->
<targetDirectory>${project.build.directory}/generated/wasm/teavm-wasm</targetDirectory>
<!-- Main class, containing static void main(String[]) -->
<mainClass>org.teavm.Scorer</mainClass>
<!-- Whether TeaVM should produce debug information for its built-in debugger -->
<debugInformationGenerated>true</debugInformationGenerated>
<targetType>WEBASSEMBLY</targetType>
<!-- Optimization level. Valid values are: SIMPLE, ADVANCED, FULL -->
<optimizationLevel>FULL</optimizationLevel>
<minHeapSize>1</minHeapSize>
<maxHeapSize>16</maxHeapSize>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This is basically a combination of the "Getting Started" .pom file and the .pom file from the benchmark sample located here: https://github.com/konsoletyper/teavm/blob/master/samples/benchmark/pom.xml
I added the second execution block to the teavm plugin configuration section. Everything else in the benchmark .pom looks like it isn't related to .wasm file generation.
When I try to build (using mvn clean package, same as I used for the js-only version of this), I get the following error:
[INFO] Running TeaVM
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.876 s
[INFO] Finished at: 2022-02-24T18:43:21-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.teavm:teavm-maven-plugin:0.6.1:compile (wasm-client) on project teavm-maven-webapp: Unexpected error occurred: Array index out of range: 0 -> [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
Does anyone know what might be causing this? I checked the apache wiki page url in the error log and it doesn't really help. I've never used either TeaVM or Maven before and this has me kind of stumped.