更新:我下面的原始答案很不充分,因为我花了三年的时间开发FastClasspathScanner,并提交了大量关于某些类路径环境无法使用该库的错误报告。 FastClasspathScanner现在可以处理许多复杂的类路径规范机制。 在一般情况下(即使扫描它),即使只是找到类路径也可能会变得异常复杂,因为有很多方法可以将jar和目录添加到类路径。
一方面,我在下面提供的代码只能处理URLClassLoader,并且许多主要的运行时环境和容器都没有对此进行扩展,它们从头实现了自己的类加载器。 但是,在Java 9+的情况下,它变得比这复杂得多,因为即使传统的类路径仍然存在,未来的一切都将朝着使用模块路径而非类路径发展。 模块具有URL,但它们是"jrt:/" URL,而不是"file:/" URL,并且模块URL实际上不包括文件路径,仅包含模块名称-因此,您甚至不能在磁盘上找到该模块。 您唯一的选择是使用(高度封装的)模块系统来处理模块。 我在这里写了有关如何扫描模块路径的文章。
FastClasspathScanner处理许多复杂的类路径规范机制,因此您无需重新发明轮子。 您可以从FastClasspathScanner中获取类路径条目的列表-这将为您省去尝试与在野外发现的所有各种类路径规范机制一起使用的麻烦。 (如果最后一个链接断开,我们深表歉意– FCS的API和文档将很快更改。)
--
[旧答案-已过时:]
其他答案在大多数情况下都是正确的,但比某些情况下要复杂得多。 Maven,Tomcat和JUnit具有自己的类路径支持,并且不使用系统类路径。
到目前为止,这是我设法提供的最完整的系统(此代码来自我的Fast Classpath Scanner项目):
/** The unique elements of the classpath, as an ordered list. */
private final ArrayList classpathElements = new ArrayList<>();
/** The unique elements of the classpath, as a set. */
private final HashSet classpathElementsSet = new HashSet<>();
/** Clear the classpath. */
private void clearClasspath() {
classpathElements.clear();
classpathElementsSet.clear();
}
/** Add a classpath element. */
private void addClasspathElement(String pathElement) {
if (classpathElementsSet.add(pathElement)) {
final File file = new File(pathElement);
if (file.exists()) {
classpathElements.add(file);
}
}
}
/** Parse the system classpath. */
private void parseSystemClasspath() {
// Look for all unique classloaders.
// Keep them in an order that (hopefully) reflects the order in which class resolution occurs.
ArrayList classLoaders = new ArrayList<>();
HashSet classLoadersSet = new HashSet<>();
classLoadersSet.add(ClassLoader.getSystemClassLoader());
classLoaders.add(ClassLoader.getSystemClassLoader());
if (classLoadersSet.add(Thread.currentThread().getContextClassLoader())) {
classLoaders.add(Thread.currentThread().getContextClassLoader());
}
// Dirty method for looking for any other classloaders on the call stack
try {
// Generate stacktrace
throw new Exception();
} catch (Exception e) {
StackTraceElement[] stacktrace = e.getStackTrace();
for (StackTraceElement elt : stacktrace) {
try {
ClassLoader cl = Class.forName(elt.getClassName()).getClassLoader();
if (classLoadersSet.add(cl)) {
classLoaders.add(cl);
}
} catch (ClassNotFoundException e1) {
}
}
}
// Get file paths for URLs of each classloader.
clearClasspath();
for (ClassLoader cl : classLoaders) {
if (cl != null) {
for (URL url : ((URLClassLoader) cl).getURLs()) {
if ("file".equals(url.getProtocol())) {
addClasspathElement(url.getFile());
}
}
}
}
}
/** Override the system classpath with a custom classpath to search. */
public FastClasspathScanner overrideClasspath(String classpath) {
clearClasspath();
for (String pathElement : classpath.split(File.pathSeparator)) {
addClasspathElement(pathElement);
}
return this;
}
/**
* Get a list of unique elements on the classpath (directories and files) as File objects, preserving order.
* Classpath elements that do not exist are not included in the list.
*/
public ArrayList getUniqueClasspathElements() {
return classpathElements;
}