• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting classes in a package programatically

 
Serkan Demir
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we get the list of Class objects in a given package?


public Class[] getClasses(String packageName) {
..........
}
 
Mohit Singhaal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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);
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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 ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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
  • 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).
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for explaining it, Ernest Friedman-Hill. Got your point
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
thanks guys for all messages.
 
Daanish Suhail
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic