aspose file tools*
The moose likes Java in General and the fly likes Getting classes in a package programatically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting classes in a package programatically" Watch "Getting classes in a package programatically" New topic
Author

Getting classes in a package programatically

Serkan Demir
Ranch Hand

Joined: Feb 03, 2005
Posts: 61
Can we get the list of Class objects in a given package?


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


A guy from Turkey<br /><a href="http://blogspot.serkandemir.com" target="_blank" rel="nofollow">http://blogspot.serkandemir.com</a>
Mohit Singhaal
Greenhorn

Joined: Jul 26, 2006
Posts: 1
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

Joined: Jul 20, 2005
Posts: 10090
    
164

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 ]

[My Blog] [JavaRanch Journal]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10090
    
164

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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10090
    
164

Thank you for explaining it, Ernest Friedman-Hill. Got your point
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Feb 03, 2005
Posts: 61
thanks guys for all messages.
Daanish Suhail
Greenhorn

Joined: Aug 24, 2012
Posts: 10
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting classes in a package programatically