• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Getting classes in a package programatically

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we get the list of Class objects in a given package?


public Class[] getClasses(String packageName) {
..........
}

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class java.lang.Package does not have a fascility to return
the classes that are contained in a particular package.

For example:

static Class [] getClasses(String packageName);
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Incidentally, i was trying to do the same just yesterday and here's what i found from some site:




There are no direct APIs to achieve this result. Also, the above code will not work if your classes are present in a jar file(code might need some modification to achieve this)
[ July 28, 2006: Message edited by: jaikiran pai ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Serkan Demir:
Can we get the list of Class objects in a given package?



No, the number of classes that exists in a given package is finite at a given point in time, but infinite over time. Simply, there is no way to know of all classes within a package unless you define a finite context, such as a class loader.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No, the number of classes that exists in a given package is finite at a given point in time, but infinite over time



I had heard about this in some forum discussion where the reason why its not possible was mentioned, but i could not understand what it meant. Here's the extract from that post:

It's not possible to write a getClasses method on Package because this information simply isn't available. If I load my classes over a socket, how is the client supposed to divine all of the class names on the (not necessarily Java based) server ?



Can anyone explain what this means and why exactly is it not possible?
Thank you.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
Simply, there is no way to know of all classes within a package unless you define a finite context, such as a class loader.



Even if you do specify that you want to know all the classes available to a particular ClassLoader, you still can't get them, can you? At least, not by any method that works for all ClassLoaders.

If you want to do it for the system ClassLoader (the one that reads classes from the classpath), it is possible to find the list. But you have to do lots of coding yourself, as far as I know. The classpath is available as system property "java.class.path". You could parse the classpath and obtain a list of Jars and a list of directories. You could read all the Jars and all the directories, to see what was in them. That would approximate to a list of all classes available on the classpath. You could then filter that by a particular package.

For other ClassLoaders, it may or may not be possible, depending on the facilities provided by the specific ClassLoader.

Overall, it's at best tedious and at worst impossible to do what you want. So, perhaps you should take a step back and ask why you think you need to do it. Perhaps tell us, and we may be able to suggest an alternative.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:
Even if you do specify that you want to know all the classes available to a particular ClassLoader, you still can't get them, can you? At least, not by any method that works for all ClassLoaders.



Sure it is.
You can even use a "Java agent" (see java.lang.instrumentation).
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jaikiran pai:

Can anyone explain what this means and why exactly is it not possible?



Imagine that you're using a URLClassLoader to load a package. The *.class files are just sitting loose, separately, in a directory on a Web server. Directory listing is turned off for that server.

Now, if you know that class com.disney.Snarfblatt is on the server, the ClassLoader can try

GET /classes/com/disnet/Snarfblatt.class HTTP/1.0

and if the class file is there, it will be loaded.

Now, let's say that ClassLoader had a method "Class[] allClassesInPackage(Package)". Can you explain to me how it would be implemented? How could URLClassLoader get the needed listing of the remote /classes/com/disney directory?

Answer: it can't. It's not possible.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for explaining it, Ernest Friedman-Hill. Got your point
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:

Sure it is.
You can even use a "Java agent" (see java.lang.instrumentation).



Is this new in Java 5? I'm mostly stuck on 1.4.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:


Is this new in Java 5? I'm mostly stuck on 1.4.



Yes it is.
It is actually java.lang.instrument
 
Serkan Demir
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys for all messages.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:Incidentally, i was trying to do the same just yesterday and here's what i found from some site:




There are no direct APIs to achieve this result. Also, the above code will not work if your classes are present in a jar file(code might need some modification to achieve this)
[ July 28, 2006: Message edited by: jaikiran pai ]



What do send as an argument when its the default package, as in the 'default package' in eclipse?
 
no wonder he is so sad, he hasn't seen this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic