Find Where Java Class Is Loaded From

Find where java class is loaded from

Here's an example:

package foo;

public class Test
{
public static void main(String[] args)
{
ClassLoader loader = Test.class.getClassLoader();
System.out.println(loader.getResource("foo/Test.class"));
}
}

This printed out:

file:/C:/Users/Jon/Test/foo/Test.class

Where on the file system was my Java class loaded from?

Simply run java using the standard command line switch"-verbose:class" (see the java documentation). This will print out each time a class is loaded and tell you where it's loaded from.

In eclipse, how can I see where the java classes are loaded from?

To see location of classes loaded : In Run configurations -> Arguments tab add vm argument -verbose. That will include the full path of classes loaded.

EDIT:
To find a particular class from a list of jars:

I use a jar class finder plugin. I do not recall the exact location from where i downloaded, the alphaworks link seems broken as of now. But you can find similar tools.

Note : In eclipse, if the console fills up to a point you are not able to see previous logs, then in Common tab -> Standard I/O check File option.

Determine which JAR file a class is from

Yes. It works for all classes except classes loaded by bootstrap classloader. The other way to determine is:

Class klass = String.class;
URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class");

As notnoop pointed out klass.getResource() method returns the location of the class file itself. For example:

jar:file:/jdk/jre/lib/rt.jar!/java/lang/String.class
file:/projects/classes/pkg/MyClass$1.class

The getProtectionDomain().getCodeSource().getLocation() method returns the location of the jar file or CLASSPATH

file:/Users/home/java/libs/ejb3-persistence-1.0.2.GA.jar
file:/projects/classes

Getting filesystem path of class being executed

The following code snippet will do this for you:

final File f = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());

replace MyClass with your main class

The resulting File object f represents the .jar file that was executed. You can use this object to get the parent to find the directory that the .jar is in.

In Java, is it possible to know whether a class has already been loaded?

(Thanks to Aleksi) This code:

public class TestLoaded {
public static void main(String[] args) throws Exception {
java.lang.reflect.Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", new Class[] { String.class });
m.setAccessible(true);
ClassLoader cl = ClassLoader.getSystemClassLoader();
Object test1 = m.invoke(cl, "TestLoaded$ClassToTest");
System.out.println(test1 != null);
ClassToTest.reportLoaded();
Object test2 = m.invoke(cl, "TestLoaded$ClassToTest");
System.out.println(test2 != null);
}
static class ClassToTest {
static {
System.out.println("Loading " + ClassToTest.class.getName());
}
static void reportLoaded() {
System.out.println("Loaded");
}
}
}

Produces:

false
Loading TestLoaded$ClassToTest
Loaded
true

Note that the example classes are not in a package. The full binary name is required.

An example of a binary name is "java.security.KeyStore$Builder$FileBuilder$1"



Related Topics



Leave a reply



Submit