|
-
Jan 9th, 2004, 07:51 AM
#1
Thread Starter
Addicted Member
All Classes in a Package Programatically
Hello
I've had a quick look over the Java API but found nothing that caught my attention that would solve my prob, so thought i'd try you guys and see if you had any ideas.
I Need to find out the names of all the classes in a particular package at runtime, so i can populate a ComboBox.
e.g.
package uk.co.me.stuff
contains
class A
class B
class C
i'm sure you get the pitcure, I just need the String representation of the class name..
ideas on a postcard
Cheers
Andy
-
Jan 10th, 2004, 05:29 AM
#2
No-can-do on the API level. You could search the whole classpath for .class files.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 10th, 2004, 02:47 PM
#3
Dazed Member
Yah CornedBee is right. I tried to do this a while ago also. The Java Api defines a Package class but offers no implementation which would allow you to list the classes contained within.
If the you wanted to find other information about a Package at runtime where you happened to be running a class from that package you could just call Package getPackage() on an instance of this class which would allow you to get information such as the title of the package using String getImplementationTitle(), whether the package is sealed public boolean isSealed() ect ect......
-
Jan 10th, 2004, 03:07 PM
#4
Thread Starter
Addicted Member
bummer
hrm, seems a shame that you can't interogate a package.....
having not tried the package API yet, does it return the fully qualified package name like uk.co.blah.stuff.Class.class, or just the name at the end e.g. Class...
if it give me the fully qualified one, what do you reckon to then scanning the directory using the File API to find out the .class files?! a little crude, but it might work.. or am i missing something? (help)
i supose it would kind of work, but then again its not ideal.......
Sounds like i'll have to find some other ingenius way to achieve it..
Cheers for your help guys
Andy
-
Jan 10th, 2004, 04:14 PM
#5
Dazed Member
Yeah i guess you could use the File class to scan a directory for any classes contained within. A bit crude but it would work.
Code:
import java.io.*;
public class J{
public static void main(String[] args){
File f = new File("C:" + File.separator + "TestFolder");
String[] files = f.list();
for(int i = 0; i < files.length; ++i){
if(files[i].endsWith(".class")){
System.out.println(files[i]);
}
}
}
}
-
Jan 10th, 2004, 06:27 PM
#6
You can use the java.classes (I think) system property to find out the classpath.
E.g. c:\classes;c:\libs;c:\jars\myjar.jar
You can emulate the class loader and construct a subpath from the package name.
E.g. com.cornedbee.jknowledger -> com\cornedbee\jknowledger
Then you combine the two parts.
c:\classes\com\cornedbee\jknowledger
c:\libs\com\cornedbee\jknowledger
c:\jars\myjar.jar!com\cornedbee\jknowledger
Then you list the .class files in these directories.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 11th, 2004, 07:59 AM
#7
Thread Starter
Addicted Member
Marvellous
exactly what i was thinking, cheers guys, a bit crude but in the absence of API calls to do it it looks like thats the way to go!
Thanks again
Andy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|