Home > Enterprise >  java.lang.NoClassDefFoundError: sun/reflect/Reflection while running from jar
java.lang.NoClassDefFoundError: sun/reflect/Reflection while running from jar

Time:09-02

I built a jar using Ecplise, setting the main class properly.

When I run "java -Xms512m -Xmx1024m -jar foo.jar config.ini" from the command line (Windows), I get an below exception

Exception in thread "main" java.lang.NoClassDefFoundError: sun/reflect/Reflection
        at com.jidesoft.plaf.UIDefaultsLookup.getCallerClassLoader(Unknown Source)
        at com.jidesoft.plaf.UIDefaultsLookup.get(Unknown Source)
        at com.jidesoft.plaf.vsnet.VsnetWindowsUtils.initComponentDefaultsWithMenu(Unknown Source)
        at com.jidesoft.plaf.LookAndFeelFactory.installJideExtension(Unknown Source)
        at com.jidesoft.plaf.LookAndFeelFactory.installJideExtension(Unknown Source)
        at com.jidesoft.plaf.LookAndFeelFactory.installJideExtension(Unknown Source)
        at com.jidesoft.swing.JideButton.updateUI(Unknown Source)
        at java.desktop/javax.swing.AbstractButton.init(AbstractButton.java:2136)
        at java.desktop/javax.swing.JButton.<init>(JButton.java:131)
        at java.desktop/javax.swing.JButton.<init>(JButton.java:85)
        at com.jidesoft.swing.JideButton.<init>(Unknown Source)
        at com.jidesoft.swing.JideButton.<init>(Unknown Source)
        at ro.sync.ui.application.lb.<init>(Unknown Source)
        at ro.sync.exml.m.d.<init>(Unknown Source)
        at ro.sync.exml.m.d.dqe(Unknown Source)
        at ro.sync.exml.m.c.<init>(Unknown Source)
        at ro.sync.ecss.extensions.api.component.AuthorComponentFactory$2.<init>(Unknown Source)
        at ro.sync.ecss.extensions.api.component.AuthorComponentFactory.init(Unknown Source)
        at ro.sync.ecss.extensions.api.component.AuthorComponentFactory.init(Unknown Source)
        at ro.sync.ecss.samples.AuthorComponentSample.<init>(AuthorComponentSample.java:311)
        at ro.sync.ecss.samples.AuthorComponentSample.main(AuthorComponentSample.java:545)
Caused by: java.lang.ClassNotFoundException: sun.reflect.Reflection
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 21 more

CodePudding user response:

If you were really using Java 8 to run this, your JAR would probably work just fine ...

But the evidence suggests that you are actually running the code in a Java 9 or later JVM, where the "missing" sun.reflect.Reflection class has been moved to the jdk.unsupported module.

The simplest solution will be to run this on Java 8 only. Alternatively, you will need to figure out where in your code-base (or dependencies) the sun.reflect.Reflection class is being used. (It looks like it is in com.jidesoft.plaf code.)

  • If it is your code that is using the class, figure out how to do what Reflection is doing ... without relying on internal APIs.

  • If it is a dependency, look for a newer version of the dependency that supports Java 9 and later.


This may help you work around the (apparent) Java 9 compatibility issues in the jidesoft code.

  • It looks like you may be using the JIDE Common Layer. There is version of this on GitHub in the jidesoft/jide-oss

  • There is a file in the repo called README JDK9.txt. It says the following:

    --add-exports java.desktop/com.sun.java.swing.plaf.windows=ALL-UNNAMED
    --add-exports java.desktop/javax.swing.plaf.synth=ALL-UNNAMED 
    --add-exports java.desktop/sun.swing=ALL-UNNAMED 
    --add-exports java.desktop/sun.awt=ALL-UNNAMED 
    --add-exports java.desktop/sun.awt.image=ALL-UNNAMED 
    --add-exports java.desktop/sun.awt.shell=ALL-UNNAMED 
    --add-exports java.desktop/sun.awt.dnd=ALL-UNNAMED 
    --add-exports java.desktop/sun.awt.windows=ALL-UNNAMED 
    --add-exports java.base/sun.security.action=ALL-UNNAMED
    
    Remove xerces.jar from the classpath
    

    These would appear to be instructions on how to use the dependency on Java 9 or later.

  • In the repo there also seem to be versions of the source code for Java 8 and Java 9.


I'm afraid, I can't tell you how to build and run the Java 9 version of the code. If it is too hard for you to figure it out, my advice is to run your application on Java 8.

Finally, there are things in the jide-oss issue tracker that suggest that this code is unlikely to get much "love" in the future. In particular, there doesn't appear to be any enthusiasm for addressing the various problems caused by the codebase's reliance on JDK internal APIs. This will be increasingly problematic as the Java team close off access to those APIs. So I would advise:

  • Start looking for alternatives for the functionality that the jidesoft code is providing you.
  • Don't develop new applications that rely on jide-oss.
  • For an existing application, either plan to rework the code to use the alternative, or resolve to make Java 8 (and no higher!) the required platform for your application.
  • Related