我要评分
获取效率
正确性
完整性
易理解

Error: module java.base does not "opens java.lang" to unnamed module

Symptom

When the project uses JDK 11 or later, an error is reported stating "module java.base does not "opens java.lang" to unnamed module."
Figure 1 Error message

Possible Causes

Startup parameters are not added to the pom.xml or build.gradle file.

Troubleshooting Procedure

Add compilation startup parameters to the pom.xml or build.gradle file.

For example, add compilation startup parameters to the pom.xml file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<build>
    <plugins>
        <plugin>
            <!-- Compilation plugin -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
                <compilerArgs>
                    <arg>--add-exports</arg>
                    <arg>java.desktop/sun.awt.util=ALL-UNNAMED</arg>
                </compilerArgs>
            </configuration>
        </plugin>
        <plugin>
            <!-- Test plugin -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <argLine>
                    --add-opens java.base/java.lang=ALL-UNNAMED
                    --add-opens java.base/java.lang.reflect=ALL-UNNAMED
                    --add-opens java.desktop/sun.awt.util=ALL-UNNAMED
                </argLine>
            </configuration>
        </plugin>
    </plugins>
</build>