Ermitteln woher eine Klasse geladen wurde

Thomas Darimont

Erfahrenes Mitglied
Hallo!

Über
Code:
   System.out.println(org.eclipse.core.runtime.adaptor.EclipseStarter.class.getProtectionDomain().getCodeSource());
Kann man heraus bekommen, woher (aus welchem Jar/ welchem Verzeichnis) eine bestimmte Klasse geladen wurde. (Wenn man die URL zum jar/Verzeichnis haben will muss man an der CodeSource noch getLocation() aufrufen...)

Code:
   /**
    * 
    */
   package de.tutorials.osgi;
   
   import java.lang.management.ManagementFactory;
   
   import com.sun.crypto.provider.HmacMD5;
   
   /**
    * @author Tom
    *
    */
   public class OSGIExample {
   
   	/**
   	 * @param args
   	 */
   	public static void main(String[] args) throws Exception{
   		System.out.println(ManagementFactory.getRuntimeMXBean().getBootClassPath());
   		printCodeSource(HmacMD5.class);
   	}
   	
   	static void printCodeSource(Class clazz){
   		System.out.println(clazz.getProtectionDomain().getCodeSource());
   	}
   
   }
... scheint ganz so, als ob diese Methode alle CodeSourcen zu klassen ermitteln kann, die nicht im BootClassPath liegen. Ruft man getProtectionDomain().getCodeSource() an einer Klasse auf die im BootClassPath liegt bekommt man /scheinbar/ immer null zurück. Kann das jemand bestätigen?

gruss Tom
 
Zurück