为什么我会收到 sun.misc.Contended 符号找不到错误?

Why am I getting sun.misc.Contended symbol cannot find error?

我有一个非常简单的程序:

Test.java:

package com;
import sun.misc.Contended;
public class Test {
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>1.9</maven.compiler.source>
    <maven.compiler.target>1.9</maven.compiler.target>
</properties>
</project>

当我输入 mvn compile 时,我得到:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project proj: Compilation failure
[ERROR] /home/john/Desktop/proj/src/main/java/com/Test.java: cannot find symbol
[ERROR] symbol:   class Contended
[ERROR] location: package sun.misc

为什么?

因为,sun.misc 移到了 jdk.internal.misc 并且它们是 JDK 的私人包裹。

... friend interfaces should be moved out of 'sun.misc' and located in a truly private package. This is so that they are not part of the proposed to be exported 'sun.misc' package.

您可以使用 --add-exports--add-opens 访问内部 API。

对于您的情况,您可以在 pom.xml

中添加以下内容
<compilerArgs>
   <arg>--add-exports</arg>
   <arg>java.base/jdk.internal.misc=<<your.module>></arg>
</compilerArgs>

请注意,您需要将 <<your.module>> 更改为您的实际模块名称。

参考:- JEP-261:Module System