Obtain from where a Class is loadedFrom WikiJava
This tutorial contains two methods to dinamically find where a class is loaded from.
the articleThere are two ways to obtain from where a class is loaded, first methodpublic class LoadingFromWhere { public static void main(String args[]){ LoadingFromWhere s = new LoadingFromWhere(); s.doit(); } public void doit() { System.out.println(this.getClass().getName() + " is loaded from " + getClass().getProtectionDomain().getCodeSource().getLocation()); MyClass s = new MyClass(); } } class MyClass { MyClass() { System.out.println (this.getClass().getName() + " is loaded from " + this.getClass().getProtectionDomain().getCodeSource().getLocation()); } } gives as output >java LoadingFromWhere LoadingFromWhere is loaded from file:/C:/temp/ MyClass is loaded from file:/C:/temp/
second methodthis method doesn't work with Jars. public class FromWhere { public static void main(String args[]){ Class theClass = FromWhere.class; java.net.URL u = theClass.getResource(""); System.out.println("This class (FromWhere) is located at : " + u); } } gives as output > java FromWhere This class (FromWhere) is located at : file:/C:/temp/ See Also |
