Obtain from where a Class is loadedTag(s): Language Varia
This HowTo detects where the class is coming from.
public 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()); } }
C:/temp>java LoadingFromWhere LoadingFromWhere is loaded from file:/C:/temp/ MyClass is loaded from file:/C:/temp/
C:/temp>java -jar testing.jar LoadingFromWhere is loaded from file:/C:/temp/testing.jar MyClass is loaded from file:/C:/temp/testing.jar
Other technique
package com.rgagnon; 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); } }
C:/temp> java FromWhere This class (FromWhere) is located at : file:/C:/temp/com/rgagnon
See these related HowTo's :