1 56 57 59 import java.net.URL ; 60 61 class WhichClass { 62 public static void main(String [] args) { 63 String targetClass = null; 64 if (args.length == 1) { 65 targetClass = args[0]; 66 } 67 else { 68 printUsage(); 69 return; 70 } 71 72 try { 73 Class.forName(targetClass); 74 System.out.println("Found class '" + targetClass + "'"); 75 } 76 catch (ClassNotFoundException ex){ 77 System.out.println("Failed to find class '" + targetClass + "'"); 78 } 79 80 URL u = ClassLoader.getSystemResource(toPath(targetClass)); 81 if (u != null) { 82 System.out.println("at URL '" + u + "'"); 83 } 84 } 85 86 private static String toPath(String className){ 87 StringBuffer sb = new StringBuffer (className); 88 for (int i=0; i < sb.length(); i++) { 89 if (sb.charAt(i) == '.') { 90 sb.setCharAt(i, '/'); 91 } 92 } 93 sb.append(".class"); 94 return sb.toString(); 95 } 96 97 private static void printUsage() { 98 System.out.println("This program reports the location of a class file."); 99 System.out.println("Usage: java WhichClass classname"); 100 } 101 } 102 103 | Popular Tags |