java 在Maven项目中使用jdk.incubator.vector的问题

zi8p0yeb  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(395)

我试图弄清楚如何在我的Maven项目中使用jdk.incubator.vector,但它总是返回以下错误:

Caused by: java.lang.Error: Unresolved compilation problems: 
    The import jdk.incubator cannot be resolved
    The import jdk.incubator cannot be resolved
    VectorSpecies cannot be resolved to a type
    FloatVector cannot be resolved to a variable
    VectorSpecies cannot be resolved to a type
    DoubleVector cannot be resolved to a type
    DoubleVector cannot be resolved
    VectorSpecies cannot be resolved to a type
    .....

这是我的pom.xml文件:

<build>
        <plugins>
            <!-- Use JDK 17, instead of the default one (JDK 1.5) -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>17</release>
                    <compilerArgs>
                        <arg>--add-modules</arg>
                        <arg>jdk.incubator.vector</arg>
                    </compilerArgs>
                </configuration>
            </plugin>

            <!-- Testing Settings -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <systemPropertyVariables>
                        <org.vanilladb.core.config.file>${project.build.directory}/test-classes/org/vanilladb/core/vanilladb.properties</org.vanilladb.core.config.file>
                        <java.util.logging.config.file>${project.build.directory}/test-classes/java/util/logging/logging.properties</java.util.logging.config.file>
                    </systemPropertyVariables>
                    <forkCount>1</forkCount>
                    <argLine>--add-modules</argLine>
                    <argLine>jdk.incubator.vector</argLine>
                    <includes>
                        <include>org/vanilladb/core/FullTestSuite.java</include>
                    </includes>
                </configuration>
            </plugin>
....

然后我将代码编译成一个可运行的jar文件,并运行它。我的run参数如下所示:

java -jar --add-modules=jdk.incubator.vector -Djava.util.logging.config.file=logging.properties -Dorg.vanilladb.bench.config.file=vanillabench.properties -Dorg.vanilladb.core.config.file=vanilladb.properties server.jar baseline_DB

我是Maven的新手,有人能帮我解决这个问题吗?先谢谢你了。

8nuwlpux

8nuwlpux1#

surefire/failsafe/compiler插件的配置:
使用following versions of maven plugins

  • maven-compiler-plugin版本3.11.0
  • maven-surefire-plugin版本3.1.2
  • maven-failsafe-plugin版本3.1.2
<properties>
    <maven.compiler.release>21</maven.compiler.release>
  </properties>
  ..
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <argLine>
            --add-modules jdk.incubator.vector
          </argLine>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <configuration>
          <argLine>
            --add-modules jdk.incubator.vector
          </argLine>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <compilerArgs>
            <arg>--add-modules</arg>
            <arg>jdk.incubator.vector</arg>
          </compilerArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>

Vector API示例(在JDK 21 ea build上运行):

package com.soebes.jdk21.incubator;

import jdk.incubator.vector.FloatVector;
import jdk.incubator.vector.VectorSpecies;
import org.junit.jupiter.api.Test;

class VectorTest {

  static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_256;

  void vectorComputation(float[] a, float[] b, float[] c) {

    for (int i = 0; i < a.length; i += SPECIES.length()) {
      var m = SPECIES.indexInRange(i, a.length);
      // FloatVector va, vb, vc;
      var va = FloatVector.fromArray(SPECIES, a, i, m);
      var vb = FloatVector.fromArray(SPECIES, b, i, m);
      var vc = va.mul(va).
          add(vb.mul(vb)).
          neg();
      vc.intoArray(c, i, m);
    }
  }

  @Test
  void name() {
    float[] a = {1.0f, 2.0f, 3.0f, 4.0f};
    float[] b = {1.0f, 2.0f, 3.0f, 4.0f};
    float[] c = {1.0f, 2.0f, 3.0f, 4.0f};

    vectorComputation(a, b, c);
  }
}

你可以在这里找到一个完整的工作示例:https://github.com/khmarbaise/jdk21,在这个类中:https://github.com/khmarbaise/jdk21/blob/main/src/test/java/com/soebes/jdk21/incubator/VectorTest.java但是你必须安装最新的JDK 21 ea build。

相关问题