如何在类路径中读取Java包中的所有类?

我需要读取Java包中包含的类。这些类在classpath中。我需要直接从Java程序执行此任务。你知道一个简单的方法吗?


List<Class> classes = readClassesFrom("my.package")


达令说
浏览 668回答 3
3回答

偶然的你

您可以使用此处描述的Reflections项目它非常完整且易于使用。以上网站的简要说明:Reflections会扫描您的类路径,索引元数据,允许您在运行时查询它,并可以为项目中的许多模块保存和收集该信息。例:Reflections reflections = new Reflections(&nbsp; &nbsp; new ConfigurationBuilder()&nbsp; &nbsp; &nbsp; &nbsp; .setUrls(ClasspathHelper.forJavaClassPath()));Set<Class<?>> types = reflections.getTypesAnnotatedWith(Scannable.class);

一只萌萌小番薯

我使用这个,它适用于文件或jar档案public static ArrayList<String>getClassNamesFromPackage(String packageName) throws IOException{&nbsp; &nbsp; ClassLoader classLoader = Thread.currentThread().getContextClassLoader();&nbsp; &nbsp; URL packageURL;&nbsp; &nbsp; ArrayList<String> names = new ArrayList<String>();;&nbsp; &nbsp; packageName = packageName.replace(".", "/");&nbsp; &nbsp; packageURL = classLoader.getResource(packageName);&nbsp; &nbsp; if(packageURL.getProtocol().equals("jar")){&nbsp; &nbsp; &nbsp; &nbsp; String jarFileName;&nbsp; &nbsp; &nbsp; &nbsp; JarFile jf ;&nbsp; &nbsp; &nbsp; &nbsp; Enumeration<JarEntry> jarEntries;&nbsp; &nbsp; &nbsp; &nbsp; String entryName;&nbsp; &nbsp; &nbsp; &nbsp; // build jar file name, then loop through zipped entries&nbsp; &nbsp; &nbsp; &nbsp; jarFileName = URLDecoder.decode(packageURL.getFile(), "UTF-8");&nbsp; &nbsp; &nbsp; &nbsp; jarFileName = jarFileName.substring(5,jarFileName.indexOf("!"));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(">"+jarFileName);&nbsp; &nbsp; &nbsp; &nbsp; jf = new JarFile(jarFileName);&nbsp; &nbsp; &nbsp; &nbsp; jarEntries = jf.entries();&nbsp; &nbsp; &nbsp; &nbsp; while(jarEntries.hasMoreElements()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entryName = jarEntries.nextElement().getName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(entryName.startsWith(packageName) && entryName.length()>packageName.length()+5){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entryName = entryName.substring(packageName.length(),entryName.lastIndexOf('.'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; names.add(entryName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; // loop through files in classpath&nbsp; &nbsp; }else{&nbsp; &nbsp; URI uri = new URI(packageURL.toString());&nbsp; &nbsp; File folder = new File(uri.getPath());&nbsp; &nbsp; &nbsp; &nbsp; // won't work with path which contains blank (%20)&nbsp; &nbsp; &nbsp; &nbsp; // File folder = new File(packageURL.getFile());&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; File[] contenuti = folder.listFiles();&nbsp; &nbsp; &nbsp; &nbsp; String entryName;&nbsp; &nbsp; &nbsp; &nbsp; for(File actual: contenuti){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entryName = actual.getName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entryName = entryName.substring(0, entryName.lastIndexOf('.'));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; names.add(entryName);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return names;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java