package com.util;
public class SecondUtil {
public SecondUtil(){
System.out.println("SecondUtil");
}
}
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()");
}
}
package com;
public class Util {
public Util(){
System.out.println("this is Util.");
}
}
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)
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:
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.