views:

394

answers:

5

When using java from Matlab, is there some way to figure out from where in matlab's java class path is a class being loaded? I'm trying to diagnose a error caused by conflicting versions of the same class being used simultaneously.

Specifically, the class I'm looking for is org.apache.lucene.store.FSDirectory. It seems to be used by one of the matlab toolboxes, but I don't know which one.

A: 

Assuming that an URLClassLoader is being used, you can get the file: URL of the class file like this:

ProblemClass.class.getResource("ProblemClass.class")
Michael Borgwardt
A: 

Use the inmem function as follows:

[M,X,J] = inmem

This function returns the list of Java classes in the output argument J. (It also returns the names of all currently loaded M-files in M, and the names of all currently loaded MEX-files in X.)

Here's a sample of output from the inmem function:

[m,x,j] = inmem;

MATLAB displays:

j = 
    'java.util.Date'
    'com.mathworks.ide.desktop.MLDesktop'
Bruno Rothgiesser
Thanks, but this doesn't help me figure out exactly where java.util.Date is located on the filesystem.
weiyin
+3  A: 

From http://www.exampledepot.com/egs/java.lang/ClassOrigin.html

// Get the location of this class
  Class cls = this.getClass();
 ProtectionDomain pDomain = cls.getProtectionDomain();
  CodeSource cSource = pDomain.getCodeSource();
  URL loc = cSource.getLocation();  // file:/c:/almanac14/examples/
Thorbjørn Ravn Andersen
This works! Thanks.
weiyin
A: 

Per Thorbjørn Ravn Andersen, if j reference to a java object in Matlab, its location can be retrieved with the following line of matlab code:

j.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()
weiyin
A: 

Since 1.5 using:

java -verbose:class

Prints where each class was loaded from.

OscarRyz