2

I am trying to load with java reflection a bunch of classes. Everything seems working fine (I am handling the exception if the class is not found).

However there is a particular class that is raising another exception that is not thrown by call to the Class.forname() but by an internal method and so I cannot even catch it.

Here is my code:

try {
     URL url = Class.forName(qualifiednameOfTheClass);
} catch (ClassNotFoundException ex) {
     // ok class not found can be handled
} catch (Exception e){
     // catch every other exception just to try to get the strange exception
}

So with this code everything is working, I am using it on lots of classes and it's working (sometimes it finds it sometimes it doesn't).

However there is one case that is not working properly and I cannot understand why. If qualifiednameOfTheClass = sun.security.krb5.SCDynamicStoreConfig my code is raising an exception:

Exception in thread "mythread-1" java.lang.UnsatisfiedLinkError: no osx in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886) at java.lang.Runtime.loadLibrary0(Runtime.java:849) at java.lang.System.loadLibrary(System.java:1088) at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:67) at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:47) at java.security.AccessController.doPrivileged(Native Method) at sun.security.krb5.SCDynamicStoreConfig.(SCDynamicStoreConfig.java:39) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:190) at com.myclass.getJARFromClassForName(JavaId.java:510) at com.myclass.getJARUrl(Id.java:550) at com.myclass.collectInformation(Graph.java:366) at com.myclass.createNode(Graph.java:166) at com.myclass.Graph.(Graph.java:143) at com.myclass2.run(myclass2.java:246) at java.lang.Thread.run(Thread.java:745)

So as you can see in the error we have this strange exception that cannot be caught even with a generic catch like in my code and I cannot understand why it has been raised and what actually is this osx library (I am on linux)

EDIT: The only thing that I found is this link http://www.straub.as/java/pocketapi/index7.html but is in german and so I read it with google translate and I don't know if I got it right but is saying that the classes listed there cannot be reproduced with Class.forname() itself.

Is it true? Is there a reason why this cannot be loaded with reflection or am I doing something wrong?

duffymo
  • 305,152
  • 44
  • 369
  • 561
rakwaht
  • 3,666
  • 3
  • 28
  • 45

3 Answers3

3

"Cannot be caught" because it's an Error, not an Exception. I'd recommend reviewing the object hierarchy in the JDK for Throwable.

Try changing that to Throwable and you'll have better luck catching. I don't know why the error is happening.

This looks like a JNI class that's using native code. I don't know what you're doing, but this looks like a bad idea to me.

duffymo
  • 305,152
  • 44
  • 369
  • 561
3

This is not how this class expects to be loaded and loading this internal class directly doesn't appear to work. You need to load the class using the standard encryption API so that this class can be loaded as expected, or possibly not loaded at all (it could be code which will only work on OSX)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

As you can see in the link you have provided, there are some classes where this method fails.

When calling this method on these objects, Java needs first to load additional stuff, as this is platform specific stuff which is not shipped by default. In your case, it is Kerberos, a security API. As you can see in its Documentation, it searches for some files in specific paths (java.library.path). As it can not find it there, it throws an error.
Note that the error UnsatisfiedLinkError does not refer to finding the class name for sun.security.krb5.SCDynamicStoreConfig itself. It refers to not finding the native library in the paths provided by java.library.path.
This path itself points, for example on Windows, to C:Windows/system32/.

However, you may catch this error with catch(Error e), note that an Error is not an Exception (Throwable hierarchy). Be aware that catching an Error in general is no good idead as you can not be sure if the JVM can recover from it.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82