Java ClassLoader scanPackage(String path)

Here you can find the source of scanPackage(String path)

Description

scan Package

License

Open Source License

Declaration

public static List<String> scanPackage(String path) 

Method Source Code


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

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

public class Main {
    public static List<String> scanPackage(String path) {
        List<String> clazzs = new LinkedList<>();
        try {// w  w  w  .j a  va  2 s  . com
            path = path.replaceAll("\\.", "/");
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            Enumeration<URL> resources = classLoader.getResources(path);
            File[] files = new File(resources.nextElement().getFile()).listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    scanPackage(path + "/" + file.getName());
                } else {
                    String currentName = path.replace("/", ".") + "."
                            + file.getName().substring(0, file.getName().length() - 6);
                    clazzs.add(currentName);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return clazzs;
    }
}

Related

  1. locateFile(String name)
  2. read(String fileName)
  3. readFromFile(Object obj, String fileName)
  4. readSampleJson(String name)
  5. saveProperties(Properties properties, String name)
  6. searchByAttributeKey(String key)
  7. storePropertiesFile(String fileName, String filePath, String storeComment)
  8. toTypeArray(String[] s)