Using Reflection to get all Classes?

Discussion in 'Java' started by Julian, Apr 29, 2005.

  1. Julian

    Julian Guest

    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
     
    Julian, Apr 29, 2005
    #1
    1. Advertising

  2. (Julian) writes:

    > 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).
     
    Tor Iver Wilhelmsen, Apr 29, 2005
    #2
    1. Advertising

  3. Tor Iver Wilhelmsen wrote:

    > (Julian) writes:
    >
    >
    >>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).


    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.

    --
    John Bollinger
     
    John C. Bollinger, Apr 29, 2005
    #3
  4. Julian wrote:

    > 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.
     
    Thomas Schodt, Apr 30, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. guava
    Replies:
    1
    Views:
    2,742
    Joseph Millar
    Aug 8, 2003
  2. Jeffy
    Replies:
    2
    Views:
    2,706
    Thomas Weidenfeller
    Sep 10, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,543
    Andrew Thompson
    May 31, 2004
  4. Per Bolmstedt
    Replies:
    4
    Views:
    405
    Per Bolmstedt
    Dec 14, 2006
  5. Jason Cavett
    Replies:
    18
    Views:
    1,358
    Jason Cavett
    Mar 26, 2007
Loading...

Share This Page