Here you can find the source of getClassFilePath(String package_name, String class_name)
public static String getClassFilePath(String package_name, String class_name)
//package com.java2s; import java.io.File; import java.net.*; public class Main { public static String getClassFilePath(String package_name, String class_name) { String path = null;/*ww w . j av a2s . c o m*/ try { Class c = Class.forName(package_name + "." + class_name); path = getClassFilePath(c); } catch (ClassNotFoundException e) { System.err.println("Could not locate class " + package_name + "." + class_name); path = null; } return path; } public static String getClassFilePath(Class c) { String out = null; // check for Windoze boolean windoze = (File.separatorChar == '\\'); URL url = null; String fullyQualifiedName = c.getCanonicalName(); String shortName = c.getSimpleName(); String classRes = "/" + fullyQualifiedName.replace('.', '/') + ".class"; url = c.getResource(classRes); // Make sure that the class was loaded from a class file // in a directory and not from a jar or from an applet if (url.getProtocol().equals("file")) { try { // Get the path // Sometimes JDKs produce null for path and you have to // get it from the scheme specific part (which in a // file URI should just be the path) out = url.toURI().getPath(); if (out == null) { out = url.toURI().getSchemeSpecificPart(); } // chop off the unneeded stuff out = out.substring(0, out.length() - (shortName + ".class").length()); if (windoze && out.startsWith("/")) { out = out.substring(1); } } catch (URISyntaxException e) { // Something has gone awry. All valid URLs are URIs. throw new RuntimeException("Could not find " + fullyQualifiedName + " in classpath."); } out = out.replace('/', File.separatorChar); } else { // Not from a directory. Return null. out = null; } return out; } }