How to determine absolute path (if any) of a loaded Class?

走远了吗. 提交于 2019-12-02 21:02:45
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());      
        }
    }

Output:

/C:/Documents%20and%20Settings/argus/My%20Documents/NetBeansProjects/temp/build/classes/

Or

ClassLoader loader = GetClassPath.class.getClassLoader();
System.out.println(loader.getResource("org/life/java/so/questions/GetClassPath.class"));

Try something like this:

SomeClass.class.getResource("/" + SomeClass.class.getName() + ".class").toString();

If the class is loaded from jar the result should be something like:

jar://myjar.jar!path/to/SomeClass.class
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!