Java Class from Package getClassNamesFromPackage(String packageName)

Here you can find the source of getClassNamesFromPackage(String packageName)

Description

get Class Names From Package

License

Open Source License

Declaration

public static ArrayList<String> getClassNamesFromPackage(String packageName)
            throws IOException, URISyntaxException, ClassNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.IOException;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;

public class Main {
    public static ArrayList<String> getClassNamesFromPackage(String packageName)
            throws IOException, URISyntaxException, ClassNotFoundException {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        ArrayList<String> names = new ArrayList<String>();

        packageName = packageName.replace(".", "/");
        URL packageURL = classLoader.getResource(packageName);

        URI uri = new URI(packageURL.toString());
        File folder = new File(uri.getPath());
        File[] files = folder.listFiles();
        for (File file : files) {
            String name = file.getName();
            name = name.substring(0, name.lastIndexOf('.')); // remove ".class"
            names.add(name);//from  ww w .  j a  v a  2 s .c  om
        }

        return names;
    }
}

Related

  1. getClassesInPackage(String targetPackage)
  2. getClassNameByPackage(String packageName)
  3. getClassNamesByPkg(String pkg)
  4. getClassNamesForPackage(final Package p)
  5. getClassNamesFromPackage(String packageName)
  6. getClassNamesFromPackage(String packageName)
  7. getClassNamesPackage(String pckgname)