Using Reflection to get all Classes?

J

Julian

Hallo,

I'm currently writing a Java-Application that is supposed to offer a
sort of Class Browser (similar to the Smalltalk Class Browser).

I understood the necessarity of Reflections. It's easy to get a Class
with a known name (like: Class c = Class.forName(knownName);)
But I would like to create a Class Browser that shows all classes that
are available in the system. Is this possible? If it is, how?

Thank you very much in advance!

Julian Wissel
 
T

Tor Iver Wilhelmsen

But I would like to create a Class Browser that shows all classes that
are available in the system. Is this possible? If it is, how?

You need to navigate System.getProperty("java.class.path") and the
bootclasspath. Look in every jar, directory etc. - recursively - and
analyze .class files. Remember to "convert" nested classes (they have
a $ in their names).
 
J

John C. Bollinger

Tor said:
You need to navigate System.getProperty("java.class.path") and the
bootclasspath. Look in every jar, directory etc. - recursively - and
analyze .class files. Remember to "convert" nested classes (they have
a $ in their names).

And you must recognize that the classes you discover this way are not
the only ones that an application running on the system might be able to
load. An application may include any number of its own classes or
external libraries that come from locations not on the default
classpath. You might not find all these even if you were to run your
class browser from within the application in question. You certainly
will never find classes that an application might load over a network,
or from a database, or those that an application might synthesize at
runtime.

In general, it is impossible to find "all classes available in the
system", but if you narrow the scope (to classes available via the class
browser's own classpath, for instance) then you can achieve the goal.
 
T

Thomas Schodt

Julian said:
I'm currently writing a Java-Application that is supposed to offer a
sort of Class Browser (similar to the Smalltalk Class Browser).

I understood the necessarity of Reflections. It's easy to get a Class
with a known name (like: Class c = Class.forName(knownName);)
But I would like to create a Class Browser that shows all classes that
are available in the system. Is this possible? If it is, how?

Using reflection it is possible to get a list of all *loaded* classes
from the classloader.
Not nice, but it is possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top