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;
  final int startIdx=SystemHelpers.isRunningWindows() ? 1 : 0;
  if (classUrlProto.equals("file")) {
    final Package classPackage=klazz.getPackage();
    if (classPackage != null) {
      final String classPackagePath=classPackage != null ? classPackage.getName().replace('.','/') : "";
      result=classUrlPath.substring(startIdx,classUrlPath.lastIndexOf(classPackagePath));
    }
 else {
      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());
    }
    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);
  }
}

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 ; final int startIdx = SystemHelpers . isRunningWindows ( ) ? 1 : 0 ; if ( classUrlProto . equals ( "file" ) ) { final Package classPackage = klazz . getPackage ( ) ; if ( classPackage != null ) { final String classPackagePath = classPackage != null ? classPackage . getName ( ) . replace ( '.' , '/' ) : "" ; result = classUrlPath . substring ( startIdx , classUrlPath . lastIndexOf ( classPackagePath ) ) ; } else { 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 ( ) ) ; } 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 conceptionally the directory the default package resides in . < p > example for a class com . google . security . zynamics . bindiff . ui . main packaged in / opt / zynamics / bindiff / bin / bindiff - 5 . 0 . jar : / opt / zynamics / bindiff / bin / < p > example for a class com . google . security . zynamics . bindiff . ui . main with the file main . class residing in / home / cblichmann / dev / bindiffgui / bin / com / zynamics / bindiff / ui : / home / cblichmann / dev / bindiffgui / bin /
