java 获取类加载器_java-如何从类加载器获取类路径?
更新:我下面的原始答案很不充分,因為我花了三年的時間開發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;
}
總結
以上是生活随笔為你收集整理的java 获取类加载器_java-如何从类加载器获取类路径?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数学建模四大模型总结
- 下一篇: 爬虫之request