Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Listanje sadrzaja paketa

[es] :: Java :: Listanje sadrzaja paketa

[ Pregleda: 1895 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

braneiv

Član broj: 68650
Poruke: 52
*.dynamic.sbb.co.yu.



Profil

icon Listanje sadrzaja paketa29.05.2007. u 21:43 - pre 205 meseci
Kako mogu da izlistam klase nekog paketa? Bilo bi dobro da to mogu uraditi a da ne znam apsolutnu putanju do paketa tj. da je ne navodim u kodu (naravno, podrazumijeva se da je paket u CLASSPATH-u).
Nasao sam ovaj kod na netu ali kod mene ne radi, bilo koji paket da zadam dobijam exception "No resource for ...":

Code:

       public static Class[] getClasses(String pckgname)
            throws ClassNotFoundException {
        ArrayList<Class> classes = new ArrayList<Class>();
        // Get a File object for the package
        File directory = null;
        try {
            ClassLoader cld = Thread.currentThread().getContextClassLoader();
            if (cld == null) {
                throw new ClassNotFoundException("Can't get class loader.");
            }
            String path = '/' + pckgname.replace('.', '/');
            URL resource = cld.getResource(path);
            if (resource == null) {
                throw new ClassNotFoundException("No resource for " + path);       // kod mene je resource uvijek null
            }
            directory = new File(resource.getFile());
        } catch (NullPointerException x) {
            throw new ClassNotFoundException(pckgname + " (" + directory
                    + ") does not appear to be a valid package");
        }
        if (directory.exists()) {
            // Get the list of the files contained in the package
            String[] files = directory.list();
            for (int i = 0; i < files.length; i++) {
                // we are only interested in .class files
                if (files[i].endsWith(".class")) {
                    // removes the .class extension
                    classes.add(Class.forName(pckgname + '.'
                            + files[i].substring(0, files[i].length() - 6)));
                }
            }
        } else {
            throw new ClassNotFoundException(pckgname
                    + " does not appear to be a valid package");
        }
        Class[] classesA = new Class[classes.size()];
        classes.toArray(classesA);
        return classesA;
    }


Evo i izvor: http://forum.java.sun.com/thread.jspa?threadID=341935&start=0
 
Odgovor na temu

Dejan Lozanovic
Dejan Lozanovic
Beograd

Član broj: 691
Poruke: 2325
*.dynamic.sbb.co.yu.

Jabber: null@elitesecurity.org
Sajt: speedy-order.com


+75 Profil

icon Re: Listanje sadrzaja paketa31.05.2007. u 11:52 - pre 205 meseci



Code:

/**
    * Attempts to list all the classes in the specified package as determined
    * by the context class loader
    * 
    * @param pckgname
    *            the package name to search
    * @return a list of classes that exist within that package
    * @throws ClassNotFoundException
    *             if something went wrong
    */
   private static List<Class> getClassesForPackage(String pckgname) throws ClassNotFoundException {
       // This will hold a list of directories matching the pckgname. There may be more than one if a package is split over multiple jars/paths
       ArrayList<File> directories = new ArrayList<File>();
       try {
           ClassLoader cld = Thread.currentThread().getContextClassLoader();
           if (cld == null) {
               throw new ClassNotFoundException("Can't get class loader.");
           }
           String path = pckgname.replace('.', '/');
           // Ask for all resources for the path
           Enumeration<URL> resources = cld.getResources(path);
           while (resources.hasMoreElements()) {
               directories.add(new File(URLDecoder.decode(resources.nextElement().getPath(), "UTF-8")));
           }
       } catch (NullPointerException x) {
           throw new ClassNotFoundException(pckgname + " does not appear to be a valid package (Null pointer exception)");
       } catch (UnsupportedEncodingException encex) {
           throw new ClassNotFoundException(pckgname + " does not appear to be a valid package (Unsupported encoding)");
       } catch (IOException ioex) {
           throw new ClassNotFoundException("IOException was thrown when trying to get all resources for " + pckgname);
       }

       ArrayList<Class> classes = new ArrayList<Class>();
       // For every directory identified capture all the .class files
       for (File directory : directories) {
           if (directory.exists()) {
               // Get the list of the files contained in the package
               String[] files = directory.list();
               for (String file : files) {
                   // we are only interested in .class files
                   if (file.endsWith(".class")) {
                       // removes the .class extension
                       classes.add(Class.forName(pckgname + '.' + file.substring(0, file.length() - 6)));
                   }
               }
           } else {
               throw new ClassNotFoundException(pckgname + " (" + directory.getPath() + ") does not appear to be a valid package");
           }
       }
       return classes;
   }

 
Odgovor na temu

braneiv

Član broj: 68650
Poruke: 52
*.dynamic.sbb.co.yu.



Profil

icon Re: Listanje sadrzaja paketa31.05.2007. u 12:35 - pre 205 meseci
Hvala, ali ne radi ni ovo. Slicno kao i za gore navedeni kod ni ovdje cld.getResources(path) ne vraca nista.
Ako si testirao ovaj kod i radi, javi mi na kojoj verziji Jave radis. Kod mene je 1.5.0_08.
 
Odgovor na temu

[es] :: Java :: Listanje sadrzaja paketa

[ Pregleda: 1895 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.