找不到方法'sun.misc.Unsafe.defineClass(Ljava / lang / String; [BIILjava / lang / ClassLoader; Ljava / security / ProtectionDomain;)Ljava / lang / Class

Shr*_*rma 6 java gradle spring-boot

我正在使用jdk-10.0.2gradle 4.7,在构建项目时遇到此错误。

    Unable to find method 'sun.misc.Unsafe.defineClass(Ljava/lang/String;[BIILjava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;'.

Possible causes for this unexpected error include:
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)

The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)

Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
Run Code Online (Sandbox Code Playgroud)

inv*_*bl3 7

我遇到了同样的问题(但使用JDK 11):

找不到方法'sun.misc.Unsafe.defineClass(Ljava / lang / String; [BIILjava / lang / ClassLoader; Ljava / security / ProtectionDomain;)Ljava / lang / Class;'。

gradle 4.7您一样的版本。

我的gradle-wrapper.properties档案是:

    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-all.zip
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我使用参数更改为4.9版本的包装器distributionUrl

 distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip
Run Code Online (Sandbox Code Playgroud)

并重建项目,没有任何错误。


cmo*_*ing 0

您是否指定了此处提到的模块依赖项?从 JDK 9 开始,不安全 API 似乎被隐藏了。

因此,添加

module jdk.unsupported {
    exports sun.misc;
    opens sun.misc;
    ...
}
Run Code Online (Sandbox Code Playgroud)

对你的项目可能有帮助。