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.

Recommended Answers

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.

no I want to get a hold of the instance of the class..not just the name

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??

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

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.

may i ask where does such capturing of instances/classes find its use ?

I've done this till now..but it says not loaded to every class

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");
        }   
    }
} 

Inside main()

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()) {
        Class cl=(Class)it.next();
        System.out.println("Loaded Class : "+cl);
    }

@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.

@jamescherril thanks for your suggestions..i guess i should think of another way around then

@jwenting just for learning..nothing evil intended

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.