5
$\begingroup$

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?

$\endgroup$
5
$\begingroup$

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:

  1. Using JLinkClassLoader to recover the Java class object.
  2. Getting the class' classloader (Null indicating the system classloader).
  3. Using that classloader to get the URL of the class' binary name.

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 *)
$\endgroup$
  • $\begingroup$ This is very useful. Tks +1 $\endgroup$ – Murta Jul 27 '15 at 3:22
  • $\begingroup$ Thanks. This was useful to diagnose conflicts! $\endgroup$ – Igor Antonio Jan 25 '18 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.