Jruby: cannot link Java class com.util (java.lang.NoClassDefFoundError: com/util (wrong name: com/Util))

Created on 21 Nov 2017  路  5Comments  路  Source: jruby/jruby

  1. new Eclipse Java project, and add a class named 'SecondUtil.java' in package 'com.util'
package com.util;

public class SecondUtil {
    public SecondUtil(){
        System.out.println("SecondUtil");
    }
}
  1. add a class named 'TestMain.java' in package 'com'
package com;

import org.jruby.embed.ScriptingContainer;

public class TestMain {
    public static void main(final String[] args) {
        final ScriptingContainer sc = new ScriptingContainer();
        sc.runScriptlet("Java::com.util.SecondUtil::new()");
    }
}
  1. import lib jruby-complete-9.1.14.0.jar
  2. run TestMain, console prints 'SecondUtil', all is OK.
  3. add a class 'Util.java' in package 'com' to current project
package com;

public class Util {
    public Util(){
        System.out.println("this is Util.");
    }
}
  1. run TestMain, console prints following error:
NameError: cannot link Java class com.util (java.lang.NoClassDefFoundError: com/util (wrong name: com/Util))
  method_missing at org/jruby/javasupport/JavaPackage.java:259
          <main> at <script>:1
Exception in thread "main" org.jruby.embed.EvalFailedException: (NameError) cannot link Java class com.util (java.lang.NoClassDefFoundError: com/util (wrong name: com/Util))
    at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:131)
    at org.jruby.embed.ScriptingContainer.runUnit(ScriptingContainer.java:1307)
    at org.jruby.embed.ScriptingContainer.runScriptlet(ScriptingContainer.java:1300)
    at com.TestMain.main(TestMain.java:8)
Caused by: org.jruby.exceptions.RaiseException: (NameError) cannot link Java class com.util (java.lang.NoClassDefFoundError: com/util (wrong name: com/Util))
    at org.jruby.javasupport.JavaPackage.method_missing(org/jruby/javasupport/JavaPackage.java:259)
    at RUBY.<main>(<script>:1)
  1. change jruby-complete-9.1.14.0.jar to jruby-complete-1.7.3.jar
  2. run TestMain, it works well.
JRuby 9000 java integration regression

Most helpful comment

@javalovercn Is there a reason you closed this? I was able to reproduce locally, so I do believe this is a bug.

All 5 comments

So we are having problems locating a class when there's a package of the same-but-lowercase name.

@kares You've done a lot of tweaking and optimizing of Java integration...perhaps some search order got reversed, or we're using the package name to try to load the class?

@javalovercn Is there a reason you closed this? I was able to reproduce locally, so I do believe this is a bug.

@kares The following fixes the problem for me and does not cause any regressions. Please review. Needs a test, obviously.

diff --git a/core/src/main/java/org/jruby/javasupport/Java.java b/core/src/main/java/org/jruby/javasupport/Java.java
index e481b7d113..73e8e7326b 100644
--- a/core/src/main/java/org/jruby/javasupport/Java.java
+++ b/core/src/main/java/org/jruby/javasupport/Java.java
@@ -961,13 +961,16 @@ public class Java implements Library {
             // cannot link Java class com.sample.FooBar needs Java 8 (java.lang.UnsupportedClassVersionError: com/sample/FooBar : Unsupported major.minor version 52.0)
             throw runtime.newNameError("cannot link Java class " + className + ' ' + msg, className, ex, false);
         }
+        catch (NoClassDefFoundError | ClassNotFoundException ncdfe) {
+            // let caller try other names
+            return null;
+        }
         catch (LinkageError ex) {
             throw runtime.newNameError("cannot link Java class " + className + ' ' + '(' + ex + ')', className, ex, false);
         }
         catch (SecurityException ex) {
             throw runtime.newSecurityError(ex.getLocalizedMessage());
         }
-        catch (ClassNotFoundException ex) { return null; }

         if ( initJavaClass ) {
             return getProxyClass(runtime, JavaClass.get(runtime, clazz));

@kares I went ahead with it because everything JI-related looked ok.

I also figured out this was probably introduced when you cleaned up the RaiseException nonsense in getProxyClassOrNull in 77ef13e8c9e. Took over 2.5 years for someone to run into this case!

heh, time sure flies and bugs prevail. makes sense - thanks for the fix :purple_heart:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iconara picture iconara  路  5Comments

guizmaii picture guizmaii  路  5Comments

eregon picture eregon  路  4Comments

olleolleolle picture olleolleolle  路  5Comments

JohnPhillips31416 picture JohnPhillips31416  路  6Comments