Java find all loaded classes

Class ClassLoader

A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a «class file» of that name from a file system.

Every Class object contains a reference to the ClassLoader that defined it.

Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by Class.getClassLoader() is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.

Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.

Class loaders may typically be used by security managers to indicate security domains.

In addition to loading classes, a class loader is also responsible for locating resources. A resource is some data (a » .class » file, configuration data, or an image for example) that is identified with an abstract ‘/’-separated path name. Resources are typically packaged with an application or library so that they can be located by code in the application or library. In some cases, the resources are included so that they can be located by other libraries.

Читайте также:  PHP Contact Form

The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When requested to find a class or resource, a ClassLoader instance will usually delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself.

Class loaders that support concurrent loading of classes are known as parallel capable class loaders and are required to register themselves at their class initialization time by invoking the ClassLoader.registerAsParallelCapable method. Note that the ClassLoader class is registered as parallel capable by default. However, its subclasses still need to register themselves if they are parallel capable. In environments in which the delegation model is not strictly hierarchical, class loaders need to be parallel capable, otherwise class loading can lead to deadlocks because the loader lock is held for the duration of the class loading process (see loadClass methods).

Run-time Built-in Class Loaders

  • Bootstrap class loader. It is the virtual machine’s built-in class loader, typically represented as null , and does not have a parent.
  • Platform class loader. The platform class loader is responsible for loading the platform classes. Platform classes include Java SE platform APIs, their implementation classes and JDK-specific run-time classes that are defined by the platform class loader or its ancestors. The platform class loader can be used as the parent of a ClassLoader instance. To allow for upgrading/overriding of modules defined to the platform class loader, and where upgraded modules read modules defined to class loaders other than the platform class loader and its ancestors, then the platform class loader may have to delegate to other class loaders, the application class loader for example. In other words, classes in named modules defined to class loaders other than the platform class loader and its ancestors may be visible to the platform class loader.
  • System class loader. It is also known as application class loader and is distinct from the platform class loader. The system class loader is typically used to define classes on the application class path, module path, and JDK-specific tools. The platform class loader is the parent or an ancestor of the system class loader, so the system class loader can load platform classes by delegating to its parent.
Читайте также:  Finally java когда не работает

Normally, the Java virtual machine loads classes from the local file system in a platform-dependent manner. However, some classes may not originate from a file; they may originate from other sources, such as the network, or they could be constructed by an application. The method defineClass converts an array of bytes into an instance of class Class . Instances of this newly defined class can be created using Class.newInstance .

The methods and constructors of objects created by a class loader may reference other classes. To determine the class(es) referred to, the Java virtual machine invokes the loadClass method of the class loader that originally created the class.

For example, an application could create a network class loader to download class files from a server. Sample code might look like:

ClassLoader loader = new NetworkClassLoader(host, port); Object main = loader.loadClass("Main", true).newInstance(); . . .

The network class loader subclass must define the methods findClass and loadClassData to load a class from the network. Once it has downloaded the bytes that make up the class, it should use the method defineClass to create a class instance. A sample implementation is:

class NetworkClassLoader extends ClassLoader < String host; int port; public Class findClass(String name) < byte[] b = loadClassData(name); return defineClass(name, b, 0, b.length); >private byte[] loadClassData(String name) < // load the class data from the connection . . . >>

Binary names

Any class name provided as a String parameter to methods in ClassLoader must be a binary name as defined by The Java Language Specification .

Examples of valid class names include:

"java.lang.String" "javax.swing.JSpinner$DefaultEditor" "java.security.KeyStore$Builder$FileBuilder$1" "java.net.URLClassLoader$3$1"

Any package name provided as a String parameter to methods in ClassLoader must be either the empty string (denoting an unnamed package) or a fully qualified name as defined by The Java Language Specification .

Источник

list of classes currently loaded in JVM

how can we get the list of all the classes that are currently loaded in the Java Virtual Machine(JVM)?? it’d be better without the use of any external libraries..thank you.

  • 5 Contributors
  • 17 Replies
  • 7K Views
  • 4 Days Discussion Span
  • Latest Post 9 Years Ago Latest Post by rubberman

Well, you can see all the classes as they load with the -verbose:class option when you start the jvm. You can pipe that output to a file. After they are loaded, the application should run normally.

how can we get the list of all the classes that are currently loaded
.
no I want to get a hold of the instance of the class

So is it version 1 or version 2? And if it’s 2, how would you define «the instance of the …

By «multiple instances» I mean something like:

Object a = new Thing(); // a is an instance of Thing Object b = new Other(); // b is an instance of Other Object c = new Other(); // c is an instance of Other 

OK. That’s perfectly clear now, thanks.
I don’t know of any API that does that, but the visualvm profiling tool that comes with the JDK does have tools to get a list of all the objects in the heap (eg see http://visualvm.java.net/heapdump.html …

more thoughts.

if the target starts at boot time then it will be running in its own VM with its own heap/stack. Any other Java program you start later will also have its own VM/heap/stack — you don’t get to start your program in someone else’s already-running VM unless the …

All 17 Replies

Well, you can see all the classes as they load with the -verbose:class option when you start the jvm. You can pipe that output to a file. After they are loaded, the application should run normally.

You want a java method that can do that? RTFM! Java docs for all of this stuff is readily available on the web. Try a visit to the Oracle java language pages.

how can we get the list of all the classes that are currently loaded
.
no I want to get a hold of the instance of the class

So is it version 1 or version 2? And if it’s 2, how would you define «the instance of the class» if there are multiple instances?

@rubberman i looked at the stuffs on the web and did not understand it clear..that’s why i’m here @jamescherrill what do you mean by multiple instances?? suppose a program loads instances of two classes ClassA and ClassB into JVM..now i want to get a hold of those two instances from another program using ClassLoader or anything that works..I don’t not want to use any IPC for this. Is this possible??

Object a = new Thing(); // a is an instance of Thing Object b = new Other(); // b is an instance of Other Object c = new Other(); // c is an instance of Other 

The JVM now has loaded two classes (Thing and Other) and three instances (1 of Thing, 2 of Other). Which of these are you trying to find?

Thank you for replying..I want the all the instances that are currently loaded in the JVM..then I’ll filter the instances that i want in my program using instanceof operator.
In the above case i want all a, b and c.

OK. That’s perfectly clear now, thanks.
I don’t know of any API that does that, but the visualvm profiling tool that comes with the JDK does have tools to get a list of all the objects in the heap (eg see http://visualvm.java.net/heapdump.html ).
Maybe you can simply use that tool via ProcessBuilder, or maybe you can google around and find the VM interface that it uses, and quesy that directly.

public class MyClassLoader extends ClassLoader < public MyClassLoader() < super(MyClassLoader.class.getClassLoader()); >public void findMyClass(String className) < Class c=findLoadedClass(className); if(c==null) < System.out.println(className + " is not loaded"); >else < System.out.println(className + " is already loaded"); >> > 
String s=new String("Hello world"); MyClassLoader cl=new MyClassLoader(); cl.findMyClass("java.lang.String"); 

am I going the right way?? @somjit<> i need to change the state of the instance of the class at runtime..since I don’t have the reference to the instance of that class..I’m trying to get a reference first using findLoadedClass()..then change the state of that instance

I doubt that will work.
findLoadedClass just finds classes loaded by that ClassLoader
it finds classes, not instances of classes (see previous discussion!) If there are no references to an instance then it’s going to be garbage collected at some unpredictable time. If there is a reference then you should follow that — you can use the reflection API to get that ref from wherever it is being maintained, as long as you have some kind of ref to some related object somewhere in the appplication. Is it possible to modify the target application to maintain a Collection of objects as they are created?

I found a way to do list the name of the classes currently loaded, which is not i need..there’s a private final Vector in java.lang.ClassLoader which stores the list

 Class c=this.class.getClassLoader().getClass(); while(c!=java.lang.ClassLoader.class) < c=c.getSuperclass(); >Field f = cl1.getDeclaredField("classes"); f.setAccessible(true); Vector classes=(Vector)f.get(this.cl); Iterator it=classes.iterator(); while(it.hasNext())

@jamescherril i don’t have the references because the objects are loaded from another program which starts at bootup..i’m trying to make a utility program which updates the state of that program at runtime

I don’t want to be discouraging, but what you are trying to do sounds somewhere between «very difficult» and «impossible». If you do find a way there are quite a few people here who will want to know how! ps: I guess you are aware that private variables in the API classes may change or disappear with any minor maintenance release of the JRE.

more thoughts. if the target starts at boot time then it will be running in its own VM with its own heap/stack. Any other Java program you start later will also have its own VM/heap/stack — you don’t get to start your program in someone else’s already-running VM unless the target program has some coding to support that. do you have access to the target’s source? If so maybe you could modify it to allow value changes from outside, and place your version of the class file ahead of the original in the classpath?

it also sounds like a very dirty hack, bordering on trying to perform some sort of trojan attack.
AFAIK you indeed can’t get a list of all class INSTANCES loaded by a JVM or ClassLoader, only a list of Classes loaded.
If you think it through logically, you can also see why it is impossible to get in Java a list of class instances available in the JVM. As already pointed out, any instance that’s no longer referenced becomes available to the garbage collector.
IF such a list were obtainable however, no instance would ever be without a reference, so there could be no garbage collection. Such a list is of course available internally to the JVM, but it’s not exposed through any Java API.

Источник

Оцените статью