title: Launching Java Subprocess using parent process Classpath
body: <p>I want to launch a java subprocess, with the same java classpath and dynamically loaded classes as the current java process. The following is not enough, because it doesn't include any dynamically loaded classes:</p>
  
  <pre><code>String classpath = System.getProperty("java.class.path");
  </code></pre>
  
  <p>Currently I'm searching for each needed class with the code below.  However, on some machines this fails for some classes/libs, the source variable is null.  Is there a more reliable and simpler way to get the location of libs that are used by the current jvm process?</p>
  
  <pre><code>String stax     = ClassFinder.classPath("javax.xml.stream.Location");
  
  public static String classPath(String qualifiedClassName) throws NotFoundException {
      try {
          Class qc = Class.forName( qualifiedClassName );
          CodeSource source = qc.getProtectionDomain().getCodeSource();
          if ( source != null ) {
              URL location = source.getLocation();        
              String f = location.getPath();
              f = URLDecoder.decode(f, "UTF-8"); // decode URL to avoid spaces being replaced by %20
              return f.substring(1);
          } else {
              throw new ClassFinder().new NotFoundException(qualifiedClassName+" (unknown source, likely rt.jar)");
          }
      } catch ( Exception e ) {
        throw new ClassFinder().new NotFoundException(qualifiedClassName);
      }
  }
  </code></pre>