How can I discover which path is read to load some Java class using the command: LoadJavaClass["my.java.class"]
?
I believe all JavaClassPath[]
is scanned, but which is used?
Update 2023-06-16: It is reported that the following response no longer works when using Mathematica 13.2 on MacOS.
The following function can be used to recover the resource URL that contains a given class:
Needs["JLink`"]
InstallJava[];
LoadJavaClass["com.wolfram.jlink.JLinkClassLoader"];
classResourceUrl[className_] :=
JavaBlock @ Module[{resourceName, loader}
, resourceName = StringReplace[className, "."->"/"]~~".class"
; loader =
com`wolfram`jlink`JLinkClassLoader`classFromName[className] @ getClassLoader[] /.
Null :> com`wolfram`jlink`JLinkClassLoaderHelper`getSystemClassLoader[]
; loader@getResource[resourceName] /. url:Except[Null] :> url@toString[]
]
It works by:
JLinkClassLoader
to recover the Java class object.Null
indicating the system classloader).Here it is in action:
classResourceUrl["org.hsqldb.jdbc.JDBCDriver"]
(* jar:file:/C:/Program%20Files/Wolfram%20Research/Mathematica/10.2/
SystemFiles/Links/DatabaseLink/Java/hsqldb.jar!
/org/hsqldb/jdbc/JDBCDriver.class *)
classResourceUrl["org.rosuda.JRI.Rengine"]
(* jar:file:/C:/Program%20Files/Wolfram%20Research/Mathematica/10.2/
SystemFiles/Links/RLink/Java/JRI.jar!
/org/rosuda/JRI/Rengine.class *)
AddToClassPath @ "C:\\eclipse\\plugins\\org.junit_4.10.0\\junit.jar";
classResourceUrl["org.junit.Assert"]
(* jar:file:/C:/eclipse/plugins/org.junit_4.10.0/junit.jar!
/org/junit/Assert.class *)