title: How to load AttachProvider (attach.dll) dynamically
body: <p>I'm using <code>com.sun.tools.attach</code> from jdk's <code>tools.jar</code> and it need an specified <code>java.library.path</code> env pointing to <code>attach.dll</code> at startup to properly instatiate provider such as <code>WindowsAttachProvider</code>. For some reasons I need to dynamic loading one of bundled <code>attach.dll</code>. I'm try to use some like this:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      Path bin = Paths.get(System.getProperty("user.dir"),"bin").toAbsolutePath();
      switch (System.getProperty("os.arch")) {
          case "amd64":
              bin = bin.resolve("win64");
              break;
          default:
              bin = bin.resolve("win32");
      }
      // Dynamic setting of java.library.path only seems not sufficient
      System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + bin.toString());
      // So I try to manual loading attach.dll. This is not sufficient too.
      System.load(bin.resolve("attach.dll").toString());
      // I'm using com.sun.tools.attach in my app
      new myApp();
  }
  </code></pre>
  
  <p>If I run this out of jdk (in normall jre) it's report to me:</p>
  
  <pre><code>java.util.ServiceConfigurationError: com.sun.tools.attach.spi.AttachProvider:
  Provider sun.tools.attach.WindowsAttachProvider could not be instantiated:
  java.lang.UnsatisfiedLinkError: no attach in java.library.path
  Exception in thread "main" com.sun.tools.attach.AttachNotSupportedException:
  no providers installed
      at com.sun.tools.attach.VirtualMachine.attach(...
  </code></pre>
  
  <p>How to install attach provider without specifying <code>-Djava.library.path</code> to point <code>attach.dll</code> at startup?</p>