通过包名获取该包下的所有类

/**  
* <p>Title: TestFanShe.java</p>  
* <p>Description: </p>  
* <p>Company: www.treebear.cn</p>  
* @author chong.du  
* @date 2018年5月16日  
* @version 1.0  
*/
package test.fanshe;

import java.io.File;
import java.net.URL;
import java.util.Enumeration;

import org.junit.Test;


public class TestFanShe {

	@Test
	public void test() throws Exception {
		
		//包名
		String packageName = "test.fanshe";
		
		Enumeration<URL> urls = TestFanShe.class.getClassLoader().getResources(packageName.replace(".", "/"));

		while (urls.hasMoreElements()) {
			
			URL url = (URL) urls.nextElement();

			if ("file".equals(url.getProtocol())) {
				String realPath = url.getFile();

				File file = new File(realPath);
				if (file.exists()) {

					File[] files = file.listFiles();
					for (File f : files) {
						System.out.println(f.getName());

					}
				}
			}
		}
	}

}

  

猜你喜欢

转载自www.cnblogs.com/geekdc/p/9047686.html