Rate This Document
Findability
Accuracy
Completeness
Readability

Setting the Preview Function

For common projects, Maven projects, and Gradle projects, if you need to compile and run the project after the code reconstruction, you need to enable the preview function.

Enabling Preview in the IntelliJ IDEA CLI

  1. Enable preview during compilation.
    javac --add-modules jdk.incubator.vector YourClass.java
  2. Enable preview during running.
    java --add-modules jdk.incubator.vector YourClass

Enabling Preview in a Maven Project

  1. Configure maven-compiler-plugin in the pom.xml file.
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.13.0</version>
                <configuration>
                    <source>17</source>  <!-- Ensure that you are using JDK 17 or later. -->
                    <target>17</target>
                    <compilerArgs>
                        <arg>--add-modules</arg>
                        <arg>jdk.incubator.vector</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
  2. Configure preview for running.
    • Enable preview in the pom.xml file.
      <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>
                      <!-- Main class -->
                      <mainClass>org.xrb.Main</mainClass>
                      <arguments>
                          <!-- Enable preview. -->
                          <argument>--enable-preview</argument>
                          <!-- If there are other runtime arguments, add them one by one. -->
                          <argument>--add-modules</argument>
                          <argument>jdk.incubator.vector</argument> <!--  For example, enable the vector module. -->
                      </arguments>
                  </configuration>
              </execution>
          </executions>
      </plugin>
    • To enable preview for running, you can also click Run/Debug Configurations in the upper right corner of the project, and select Edit Configurations.

      In the Run/Debug Configurations dialog box that is displayed, if no application configuration is available, click in the upper left corner, select Application and set related parameters, as shown in Figure 1. After the configuration is complete, click Modify Options, select Add VM Options, and add --add-modules jdk.incubator.vector to the VM options, as shown in Figure 2.

      If an application configuration is available, click Modify Options in the dialog box that is displayed, select Add VM Options, and add --add-modules jdk.incubator.vector to the VM options.

      Figure 1 Configuring an application
      Figure 2 Adding a VM option

Enabling Preview in a Gradle Project

Configure compilation and runtime parameters in the build.gradle file.
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']
}