---
title: 预览功能设置
description: "针对于普通项目、Maven项目和Gradle项目，在改造后若需要编译运行，需要启用预览功能。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengdevps/development/affinityanalyzer/Java-Vector-Helper_0011.html
sourcePath: /source/zh/kunpengdevps/development/affinityanalyzer/Java-Vector-Helper_0011.html
indexId: 8c6c6b3751d4834445501de779735eeab506b504c607b61be618f3d8cb5019b181
---
# 预览功能设置

针对于普通项目、Maven项目和Gradle项目，在改造后若需要编译运行，需要启用预览功能。

#### IntelliJ IDEA命令行启用预览功能

1. 编译时启用预览。

```
javac --add-modules jdk.incubator.vector YourClass.java
```


2. 运行时启用预览。

```
java --add-modules jdk.incubator.vector YourClass
```


#### Maven项目中启用预览功能

1. 在pom.xml中配置编译时启用预览maven-compiler-plugin。

```
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>17</source>  <!-- 确保使用 JDK 17+ -->
<target>17</target>
<compilerArgs>
<arg>--add-modules</arg>
<arg>jdk.incubator.vector</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
```


2. 配置运行时启用预览。

  - 在pom.xml中配置运行时启用预览。

```
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
<configuration>
<!-- 主类 -->
<mainClass>org.xrb.Main</mainClass>
<arguments>
<!-- 启用预览特性 -->
<argument>--enable-preview</argument>
<!-- 如果有其他运行时参数，可以继续添加 -->
<argument>--add-modules</argument>
<argument>jdk.incubator.vector</argument> <!-- 举例，启用 vector 模块 -->
</arguments>
</configuration>
</execution>
</executions>
</plugin>
```


  - 运行时启用预览也可以单击项目右上角的“运行/调试配置”，选择“编辑配置”。
    打开“运行/调试配置”弹窗，若无应用程序配置，单击左上角，选择“应用程序”后，配置相关参数，如图1所示，配置完成后再单击“修改选项”，勾选“添加虚拟机选项”，并在虚拟机选项中添加“--add-modules jdk.incubator.vector”内容，如图2所示。

    若已有应用程序配置，直接单击“修改选项”，勾选“添加虚拟机选项”，并在虚拟机选项中添加“--add-modules jdk.incubator.vector”内容。

    图1 配置应用程序

    图2 添加虚拟机选项


#### Gradle项目中启用预览功能

在build.gradle文件中配置编译以及运行时参数。
```
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
options.compilerArgs += ['--add-modules', 'jdk.incubator.vector','--enable-preview']
}
tasks.withType(JavaExec).configureEach {
jvmArgs += ['--add-modules', 'jdk.incubator.vector', '--enable-preview']
}
```
