3

Is there a way to use sun.reflect in OpenJDK11, by maybe adding something in "--add-exports"? Our code fails since a jide pkg internally uses sun.reflect package and I'm trying to see if there's a way to make it work.

I've already tried with the below but that doesn't help.

"--add-exports jdk.unsupported/sun.reflect=ALL-UNNAMED"

Here's the exception, where the underlying class references sun.reflect.Reflection

java.lang.NoClassDefFoundError: sun/reflect/Reflection
Alan Bateman
  • 5,283
  • 1
  • 20
  • 25
user3723491
  • 165
  • 4
  • 12
  • Can you update the post to include the exception or at least the name of the class in sun.reflect that you are using? If it wasn't one of the critical internal APIs identified in JEP 260 then it will have been removed (or moved). – Alan Bateman Sep 26 '19 at 13:21
  • Update post with the exception. Is there a link where it lists what has been removed in OpenJDK11? Thanks. – user3723491 Sep 27 '19 at 18:38
  • 1
    sun.reflect.Reflection was terminally deprecated and was finally removed in JDK 11. Which method(s) were you using? If it was getCallerClass then you should move the standard API, meaning java.lang.StackWalker. – Alan Bateman Sep 27 '19 at 20:20

3 Answers3

1

I had this problem and fixed it by using a newer version of jide. Changing from jide-whatever:3.2.3 to jide-whatever:3.7.6 was enough to make it work in my case.

Guest
  • 11
  • 1
0

If you cannot migrate to newer versions, the solution is to make a wrapper around Throwable().getStackTrace()[n].getClass() and put it in WEB-INF/classes folder

This is simple workaround. It works in many cases.

package sun.reflect;
public class Reflection {
        public static Class<?> getCallerClass(int n){
            StackTraceElement[] elements = new Throwable().getStackTrace();
              return elements[n].getClass() ;
            }
}

https://github.com/rafaljot/NoClassDefFoundError-sun-reflect-Reflection

0

It can be fixed when you update the version of the jars.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30478926) – Nur Dec 05 '21 at 02:17