Created
October 30, 2013 03:01
-
-
Save cliff76/7226579 to your computer and use it in GitHub Desktop.
Classpath Prober
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.net.URISyntaxException; | |
| public class ClassLoaders { | |
| static void handle(Throwable e){ | |
| throw new RuntimeException("Exception " + e, e); | |
| } | |
| static void load(String toLoad) { | |
| String resRef = toLoad.replaceAll("\\.","/") + ".class"; | |
| System.out.println("Loading " + toLoad + " using resource id " + resRef); | |
| String url = null; | |
| try { | |
| url = ClassLoaders.class.getResource(resRef).toURI().toString(); | |
| } catch (URISyntaxException e) { | |
| handle(e); | |
| } | |
| System.out.println("Loader from url " + url); | |
| Class theClass=null; | |
| try { | |
| theClass = Class.forName(toLoad); | |
| } catch (ClassNotFoundException e) { | |
| handle(e); | |
| } | |
| System.out.println("Loaded by Classloader " + theClass.getClassLoader()); | |
| } | |
| public static void main(String[] args) { | |
| System.out.println("ClassLoaders.java args: " + args.length); | |
| String toLoad = (args.length > 0) ? args[0] : "org.junit.Test"; | |
| load(toLoad); | |
| System.exit(0); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment