public static String findLocalRootPath(final Class<?> klazz) {
    final String compiledFileName = klazz.getSimpleName() + ".class";
    final URL classUrl = klazz.getResource(compiledFileName);
    Preconditions.checkNotNull(
        classUrl, "Cannot find resource for class " + klazz.getCanonicalName());

    final String classUrlProto = classUrl.getProtocol();
    final String classUrlPath = classUrl.getPath();
    final String result;

    // Define a special path start index on Windows to deal with slashes before drive letters.
    final int startIdx = SystemHelpers.isRunningWindows() ? 1 : 0;
    if (classUrlProto.equals("file")) {
      // Local file system path
      final Package classPackage = klazz.getPackage();
      if (classPackage != null) {
        final String classPackagePath = classPackage.getName().replace('.', '/');
        // If the ClassLoader did load the class specified by klazz,
        // classPackagePath can always be found in resPath
        result = classUrlPath.substring(startIdx, classUrlPath.lastIndexOf(classPackagePath));
      } else {
        // Special case, find the path of a class in the default package
        result = classUrlPath.substring(startIdx, classUrlPath.lastIndexOf('/'));
      }
    } else {
      if (!classUrlProto.equals("jar")) {
        throw new RuntimeException("Cannot find root path for remote classes");
      }
      if (!classUrlPath.startsWith("file:")) {
        throw new IllegalArgumentException(
            "Cannot find root path for class " + klazz.getCanonicalName());
      }

      // The class is packaged in a JAR archive
      final String jarPath = classUrlPath.substring(5, classUrlPath.lastIndexOf('!'));

      result = jarPath.substring(startIdx, jarPath.lastIndexOf('/'));
    }

    try {
      return URLDecoder.decode(
          result.replace('/', File.separatorChar), System.getProperty("file.encoding"));
    } catch (final UnsupportedEncodingException e) {
      assert false : "Platform default URL encoding should always be available";
      throw new RuntimeException(e);
    }
  }
Find the local root path for the specified class. For classes packaged in a JAR archive, the
local root path is the directory the JAR file is contained in. For regular .class files, the
local root path is conceptually the directory the default package resides in.

<p>Example for a class com.google.security.zynamics.bindiff.ui.Main packaged in
/opt/bindiff/bin/bindiff.jar: /opt/bindiff/bin/

<p>Example for a class com.google.security.zynamics.bindiff.ui.Main with the file Main.class
residing in /home/user/Dev/BinDiffGUI/bin/com/zynamics/bindiff/ui:
/home/user/Dev/BinDiffGUI/bin/

@param klazz a {@code Class} instance for the class to find the root path for.
@return the local root path for klazz
java/zylib/src/main/java/com/google/security/zynamics/zylib/io/FileUtils.java