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
How to Sort Map Values by Key in Java
How to Validate an Xml File Against an Xsd File
Service Discovery Failed Exception Using Bluetooth on Android
How to Open a Command Terminal in Linux
Javafx: Could Not Find or Load Main Class Only on Linux
A Simple Way of Embedding a Video in My Swing Gui
How Does Linux Recognize a File as a Certain File Type, and How to Programmatically Change It
Sending Command to Java -Jar Using Stdin via /Proc/{Pid}/Fd/0
How to Add Multiple Jar Files in Classpath in Linux
What Is This Date Format? 2011-08-12T20:17:46.384Z
Hiding Strings in Obfuscated Code
How to Get Android Wifi Scan Results into a List
How to Make This Jbutton Visible? When I Have Progressive Scan Background Jwindow()
Cross-Platform Way to Open a File Using Java 1.5
Why Does Killing Jvm Also Terminates Its Child Process If Waitfor Has Been Used