title: Java Agent wont load into JVM
body: <p>Im trying to load a Java Agent into a running JVM. I have the JVMs PID but whenever I try to load the agent into the JVM I get the error <code>com.sun.tools.attach.AgentLoadException: Agent JAR not found or no Agent-Class attribute</code>. Both files are in the same directory. I compile the class and package the class into a JAR archive using <code>javac JavaAgent.java</code> and <code>jar cmf manifest.txt JavaAgent.jar *.class</code>. Any help would be appreciated, thanks in advance. </p>
  
  <p><strong>AgentInstaller</strong></p>
  
  <pre><code>package com.company;
  import com.sun.tools.attach.*;
  
  import java.io.IOException;
  
  public class AgentLoader {
  
      public static void loadAgent() {
          String agentPath = "JavaAgent.jar";
  
          System.out.println("Dynamically loading java agent");
          String pid = "9484";
  
          try {
              VirtualMachine vm = VirtualMachine.attach(pid);
              vm.loadAgent(agentPath);
              vm.detach();
          }
          catch (Exception e){
              throw new RuntimeException(e);
          }
      }
  }
  </code></pre>
  
  <p><strong>JavaAgent</strong></p>
  
  <pre><code>package com.company;
  import java.lang.instrument.Instrumentation;
  
  class JavaAgent {
      private static Instrumentation instrumentation;
  
      public static void agentmain (String args, Instrumentation inst){
          System.out.println("Java Agent inserted");
      }
  
  }
  </code></pre>
  
  <p><strong>Manifest.txt</strong></p>
  
  <pre><code>Main-Class: com.company.MyMainClass
  Agent-Class: com.company.AgentLoader
  Can-Redefine-Classes: true
  Can-Retransform-Classes: true
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thanks to apangin I am now able to load the agent but it fails to initialize <code>com.sun.tools.attach.AgentInitializationException: Agent JAR loaded but agent failed to initialize</code>. I am running a Oracle JDK.</p>