如何确定已加载类的绝对路径(如果有)? [英] How to determine absolute path (if any) of a loaded Class?
本文介绍了如何确定已加载类的绝对路径(如果有)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有(兼容的,如果可能的话)确定加载类的绝对路径?
Is there a (compatible, if possible) way to determine the absolute path of a loaded Class?
当然,这并不总是可行的(如果你认为动态创建的类),但是
如果加载的Class在jar中如何获取这个jar的绝对路径?
Of course, this is not always possible (if you think of dynamically created classes), but if the loaded Class is inside a jar how to get the absolute path for this jar?
推荐答案
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()
Fullcode:
package org.life.java.so.questions;
/**
*
* @author jigar
*/
public class GetClassPath {
public static void main(String[] args) {
System.out.println(GetClassPath.class.getProtectionDomain().getCodeSource().getLocation().getPath());
}
}
输出
/ C:/ Documents%20and%20Settings / argus / My%20Documents / NetBeansProjects / temp / build / classes /
/C:/Documents%20and%20Settings/argus/My%20Documents/NetBeansProjects/temp/build/classes/
或
ClassLoader loader = GetClassPath.class.getClassLoader();
System.out.println(loader.getResource("org/life/java/so/questions/GetClassPath.class"));
这篇关于如何确定已加载类的绝对路径(如果有)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文