title: java agent with jvmti load at runtime, unload from within
body: <p>I'm writing a Java agent to interact with JVMTI. For reasons I won't get into, I need to use JVMTI (the C interface inside the <code>java</code> process) rather than the Java APIs like <code>java.lang.instrument</code> or JDI. I would like to be able to do a couple of things which appear not to be supported directly.</p>
  
  <ol>
  <li>Is there a way to load an agent after the Java process has already started?</li>
  <li>Is there a way to unload a Java agent (short of killing the whole Java process) either from inside the JVMTI code or from outside the process? For instance, could I safely call <a href="http://man7.org/conf/lca2006/shared_libraries/slide12.html" rel="nofollow noreferrer"><code>dlclose()</code></a> from the JVMTI code if I can find the handle for the dynamically-loaded module?</li>
  </ol>
  
  <p>If these operations can't be done, is there a way to pass data to a Java agent after it's loaded? Is there a normal way to do this through some Java command-line utility? If not, can I safely create a thread and have it listen to a socket using the standard C or C++ library calls within the code for my agent?</p>
  
  <p>If it helps, don't worry about supporting Windows with your answer - I'm undertaking this project to extend a Unix-only debugging tool.</p>
  
  <p><strong>Note:</strong> I've already seen <a href="https://stackoverflow.com/questions/6124855/unloading-a-jvmti-agent-at-runtime">this</a> but thought there might be some normal way to do it that isn't in the JVMTI standard.</p>