Maven project: method defineClass in class sun.misc.Unsafe cannot be applied to given types with latest Java version

huangapple 未分类评论52阅读模式
英文:

Maven project: method defineClass in class sun.misc.Unsafe cannot be applied to given types with latest Java version

问题

我正尝试修改一个早于2010年的 mvel2 开源库代码。https://github.com/mvel/mvel

这是一个 Maven 项目。

我最近下载了源代码,但在尝试编译该项目时出现了错误消息。

  1. public Class<?> defineClassX(String className, byte[] b, int off, int len) {
  2. if (sunJVM) {
  3. return ((Unsafe) sunUnsafe).defineClass(className, b, off, len);
  4. }
  5. else {
  6. return super.defineClass(className, b, off, len);
  7. }
  8. }
  1. 错误消息错误:(59,40) java: 无法将类 sun.misc.Unsafe 中的 defineClass 方法应用于给定类型;

mvel2 2.0.16 似乎是使用 Java 1.5 构建的,但在我当前的环境中,使用的是 Java 1.8,并且 "defineClass" 方法的定义已更改。

Java 1.8 中的新方法定义:

  1. public native Class<?> defineClass(String var1, byte[] var2, int var3, int var4, ClassLoader var5, ProtectionDomain var6);

有什么解决方法吗?

我尝试过的一些选项:

  1. 为 ClassLoader 和 ProtectionDomain 传递 null 值。

  2. 在 pom.xml 中,位于:

  1. <plugins>
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-compiler-plugin</artifactId>
  5. <version>3.6.2</version>
  6. <configuration>
  7. <source>1.6</source>
  8. <target>1.6</target>
  9. <encoding>UTF-8</encoding>
  10. <compilerArgs>
  11. <compilerArg>-XDignore.symbol.file</compilerArg>
  12. </compilerArgs>
  13. </configuration>
  14. </plugin>

a) 我尝试过传递 1.5,但仍然出现相同的错误。
b) 尝试了 compilerArgs,但仍然没有成功。

  1. 在下面的评论中,VGR 提供的提示建议删除 if(sunJVM) 块,但这会导致构建 Maven 包时所有测试用例失败。

我感觉问题似乎是因为源代码包是使用 Java 1.5 构建的,现在我正在使用最新的 Java 1.8 版本构建,所以出现了错误。

有人能否提供解决这个问题的方法?

英文:

I am trying to modify an mvel2 library open source code date back from < 2010. https://github.com/mvel/mvel

THis is a maven project.

I recently downloaded the source code and when I tried to compile the project, it gives an error message.

  1. public Class&lt;?&gt; defineClassX(String className, byte[] b, int off, int len) {
  2. if (sunJVM) {
  3. return ((Unsafe) sunUnsafe).defineClass(className, b, off, len);
  4. }
  5. else {
  6. return super.defineClass(className, b, off, len);
  7. }
  8. }
  1. Error message: Error:(59,40) java: method defineClass in class sun.misc.Unsafe cannot be applied to given types;

mvel2 2.0.16 seems to have built with Java 1.5 but in my recent environment it is Java 1.8 and the "defineClass" method definition has changed.

New Method definition in Java 1.8 :

  1. public native Class&lt;?&gt; defineClass(String var1, byte[] var2, int var3, int var4, ClassLoader var5, ProtectionDomain var6);

What is the workaround for this?

Some options I tried:

  1. Passsing in null values for ClassLoaded and ProtectionDomain

  2. in pom.xml, under:

  1. &lt;plugins&gt;
  2. &lt;plugin&gt;
  3. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  4. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  5. &lt;version&gt;3.6.2&lt;/version&gt;
  6. &lt;configuration&gt;
  7. &lt;source&gt;1.6&lt;/source&gt;
  8. &lt;target&gt;1.6&lt;/target&gt;
  9. &lt;encoding&gt;UTF-8&lt;/encoding&gt;
  10. &lt;compilerArgs&gt;
  11. &lt;compilerArg&gt;-XDignore.symbol.file&lt;/compilerArg&gt;
  12. &lt;/compilerArgs&gt;
  13. &lt;/configuration&gt;
  14. &lt;/plugin&gt;
  1. a) I tried passing in 1.5 but it fails with same error
  2. b) Tried the compilerArgs but no luck either?
  1. Tip from VGR below in the comments to remove the if(sunJVM) block causes all test cases to fail when building the maven package.

I feel the issue seems to be that, since the source code package was built with Java 1.5 and now since I am building with latest Java 1.8 version, it is failing.

Can someone suggest a workaround for this?

huangapple
  • 本文由 发表于 2020年7月27日 04:13:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/63105213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定