Results 1 to 7 of 7

Thread: All Classes in a Package Programatically

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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......

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    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

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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]); 
        }
       }
      }
     }

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    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
  •  



Click Here to Expand Forum to Full Width