I'm looking for a way to print all instantiated Objects in Java. There's an easy way with PHP using get_declared_classes():
<?php
print_r(get_declared_classes());
?>
Is there an equivalent in Java, that works just as easy as in PHP?
I'm looking for a way to print all instantiated Objects in Java. There's an easy way with PHP using get_declared_classes():
<?php
print_r(get_declared_classes());
?>
Is there an equivalent in Java, that works just as easy as in PHP?
Loading classes in Java is done via class loaders, so you can only ever hope to get all classes loaded by a given class loader. Note that one class can possibly be loaded by different class loaders and that it can even be loaded in different versions by different class loaders.
If you want to find out which classes have been loaded by a single class loader, check out How can I list all classes loaded in a specific class loader
A simple solution might be
Field f = ClassLoader.class.getDeclaredField("classes");
f.setAccessible(true);
Vector<Class> classes = (Vector<Class>) f.get(classLoader);
But this depends on Suns implementation and might fail with other vendors.
Well I did a little research myself and found out that for debugging purposes the easiest way is to start the JVM with the command line option -verbose:class
like this:
java -verbose:class Example
It's going to list all instantiated classes and that does the job for me. Thanks for your help anyway.