title: AttachNotSupportedException due to missing java_pid file in Attach API
body: <p>Building a profiler of my own, I use the <a href="http://download.oracle.com/javase/1.5.0/docs/guide/jvmti/jvmti.html" rel="noreferrer">JVMTI</a> API to build a native library agent. This agent can be started together with the JVM by using the addition parameter -agentlib. In addition there is the <a href="http://download.oracle.com/javase/6/docs/technotes/guides/attach/index.html" rel="noreferrer">Attach</a> API which allows to inject an agent into a running JVM. I wanted to implement this feature to my profiler using the following code:</p>
  
  <pre><code>try {
      String pid = VirtualMachine.list().get(0).id();
      VirtualMachine vm = VirtualMachine.attach(pid);
      vm.loadAgentLibrary("agent");
  } catch (AgentLoadException e1) {
      e1.printStackTrace();
  } catch (AgentInitializationException e1) {
      e1.printStackTrace();
  } catch (IOException e1) {
      e1.printStackTrace();
  } catch (AttachNotSupportedException e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>What does it do? From all the available running virtual machines (<code>VirtualMachine.list()</code>) I choose the first one, attach to it and try to load my agent into it. The agent, on UNIX systems named libagent.so, can be found, but when trying to load the agent the following exception is thrown:</p>
  
  <pre><code>com.sun.tools.attach.AttachNotSupportedException:
     Unable to open socket file:
        target process not responding or HotSpot VM not loaded.
  </code></pre>
  
  <p>Looking into the source code, this exception is thrown, because it cannot find a file named <code>.java_pid&lt;pid&gt;</code>. I haven't found a lot information about this kind of file in the documentation. I often heard this kind of file is not used anymore, but I am running Java 1.6.</p>
  
  <p>I also tried to attach to other JVMs, in fact I kept this attaching process dynamic, for testing reasons I just try to attach to any JVM.</p>
  
  <hr>
  
  <p>This is the code which leads to the exception, taken from <a href="http://www.docjar.com/html/api/sun/tools/attach/LinuxVirtualMachine.java.html" rel="noreferrer">sun.tools.attach: LinuxVirtualMachine.java</a>:</p>
  
  <pre><code>    // Return the socket file for the given process.
      // Checks working directory of process for .java_pid&lt;pid&gt;. If not
      // found it looks in /tmp.
      private String findSocketFile(int pid) {
         // First check for a .java_pid&lt;pid&gt; file in the working directory
         // of the target process
         String fn = ".java_pid" + pid;
         String path = "/proc/" + pid + "/cwd/" + fn;
         File f = new File(path);
         if (!f.exists()) {
             // Not found, so try /tmp
             path = "/tmp/" + fn;
             f = new File(path);
             if (!f.exists()) {
                 return null;            // not found
             }
         }
         return path;
     }
  </code></pre>
  
  <p>It says, it is looking from root into the <code>/proc/&lt;pid&gt;</code> directory. Looking at a changeset of the JDK7 it seems they are making changes to the code <a href="http://hg.openjdk.java.net/jdk7/jdk7/jdk/rev/17870c6c1d4e" rel="noreferrer">JDK7 Changeset to LinuxVirtualMachine</a></p>