publicstatic URL getClassLocation(final Class cls) { if (cls == null)thrownewIllegalArgumentException("null input: cls"); URLresult=null; finalStringclsAsResource= cls.getName().replace('.', '/').concat(".class"); finalProtectionDomainpd= cls.getProtectionDomain(); // java.lang.Class contract does not specify if 'pd' can ever be null; // it is not the case for Sun's implementations, but guard against null // just in case: if (pd != null) { finalCodeSourcecs= pd.getCodeSource(); // 'cs' can be null depending on the classloader behavior: if (cs != null) result = cs.getLocation(); if (result != null) { // Convert a code source location into a full class file location // for some common cases: if ("file".equals(result.getProtocol())) { try { if (result.toExternalForm().endsWith(".jar") || result.toExternalForm().endsWith(".zip")) result = newURL("jar:".concat(result.toExternalForm()) .concat("!/").concat(clsAsResource)); elseif (newFile(result.getFile()).isDirectory()) result = newURL(result, clsAsResource); } catch (MalformedURLException ignore) {} } } } if (result == null) { // Try to find 'cls' definition as a resource; this is not // document.d to be legal, but Sun's implementations seem to //allow this: finalClassLoaderclsLoader= cls.getClassLoader(); result = clsLoader != null ? clsLoader.getResource(clsAsResource) : ClassLoader.getSystemResource(clsAsResource); } return result; } %> <html> <head> <title>srcAdd.jar</title> </head> <body bgcolor="#ffffff"> 使用方法,className参数为类的全名,不需要.class后缀,如 srcAdd.jsp?className=java.net.URL <% try { StringclassLocation=null; Stringerror=null; StringclassName= request.getParameter("className");