title: Byte Buddy - HotSwap with ByteBuddyAgent
body: <p>I'm trying to use the HotSwap feature of byte buddy. Unfortunately I receive some errors. I have read the documentation on the <a href="http://bytebuddy.net/#/tutorial" rel="nofollow noreferrer">official website</a>, and I'm aware that it only works when the program use a Java agent. I have tried to put -javaagent parameter  on the startup of the Java virtual machine which looks like this:</p>
  
  <blockquote>
    <p>-javaagent:C:\lib\byte-buddy-agent-0.5.6.jar</p>
  </blockquote>
  
  <p>This produces the following error when starting my application:</p>
  
  <pre><code>java.lang.ClassNotFoundException:
      net.bytebuddy.agent.ByteBuddyAgent.Installer
  
  FATAL ERROR in native method: processing of -javaagent failed
  
      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)   at
      java.lang.ClassLoader.loadClass(ClassLoader.java:424)   at
      sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)   at
      java.lang.ClassLoader.loadClass(ClassLoader.java:357)   at
      sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
      at
      sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
      Exception in thread "main" Java Result: 1
  </code></pre>
  
  <p>Nevertheless I tried use the <code>ByteBuddyAgent.installOnOpenJDK()</code> method instead of the -javaagent parameter in hope that will solve the problem. But this throws following error, which relies on the same problem I think:</p>
  
  <pre><code>java.lang.ClassNotFoundException: net.bytebuddy.agent.ByteBuddyAgent$Installer
      at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
      at sun.instrument.InstrumentationImpl.loadClassAndCallAgentmain(InstrumentationImpl.java:411)
  Apr 09, 2015 1:35:01 PM net.bytebuddy.agent.ByteBuddyAgent doInstall
  INFORMATION: Cannot delete temporary file: C:\Users\Flo\AppData\Local\Temp\byteBuddyAgent4745240427430305215.jar
  Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      ...
  Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      ...
  Caused by: java.lang.IllegalStateException: The programmatic installation of the Byte Buddy agent is only possible on the OpenJDK and JDKs with a compatible 'tools.jar'
      at net.bytebuddy.agent.ByteBuddyAgent.installOnOpenJDK(ByteBuddyAgent.java:176)
      at hotswapping.FXMLDocumentController.handleByteBuddyButton(FXMLDocumentController.java:90)
      ... 60 more
  Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at net.bytebuddy.agent.ByteBuddyAgent.doInstall(ByteBuddyAgent.java:199)
      at net.bytebuddy.agent.ByteBuddyAgent.installOnOpenJDK(ByteBuddyAgent.java:174)
      ... 61 more
  Caused by: com.sun.tools.attach.AgentInitializationException: Agent JAR loaded but agent failed to initialize
      at sun.tools.attach.HotSpotVirtualMachine.loadAgent(HotSpotVirtualMachine.java:121)
      ... 67 more
  </code></pre>
  
  <p>Does anyone know what is the problem or have I misunderstood something from the tutorial? Btw I tried it with jdk1.7.0_55 and jdk1.8.0_40 and use neatbeans as ide. Version of byte buddy which I am using is v0.5.6.
  Thanks for helping.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>It seems that the error with the -javaagent parameter is a bug in the current version, thanks <a href="https://stackoverflow.com/users/1237575/rafael-winterhalter">Rafael Winterhalter</a> for the quick response.</p>
  
  <p>I also figured out what was problem with the <code>ByteBuddyAgent.installOnOpenJDK()</code> method. It was a really stupid mistake from my side. It seems my netbeans use an older java version as jdk1.8.0_40, so I changed the <code>netbeans_jdkhome</code> variable in the <code>netbeans.conf</code> file in the etc folder of netbeans. Now that my netbeans use the same java version as my projects it seems that it works like a charm even with JavaFX applications. The only strange thing is that this error occurred only in JavaFX applications, in normal Java applications I never had this problems. FYI: Here is the code sample of my JavaFX application:</p>
  
  <pre><code>package testbytebuddy;
  
  import javafx.application.Application;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.StackPane;
  import javafx.stage.Stage;
  import net.bytebuddy.agent.ByteBuddyAgent;
  
  public class TestByteBuddy extends Application {
  
      @Override
      public void start(Stage primaryStage) {
          Button btn = new Button();
          btn.setText("Say 'Hello World'");
          btn.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
              @Override
              public void handle(ActionEvent event) {
                  System.out.println("Hello World!");
              }
          });
  
          StackPane root = new StackPane();
          root.getChildren().add(btn);
  
          Scene scene = new Scene(root, 300, 250);
  
          primaryStage.setTitle("Hello World!");
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          System.out.println("Start application");
          ByteBuddyAgent.installOnOpenJDK();
          launch(args);
          System.out.println("End application");
  
      }
  
  }
  </code></pre>