Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Hot Code Swapping Works

Tags:

java

I've been programming in Java, and I really enjoy the feature in Netbeans and other IDEs that allows you to make changes to your program while debugging without having to restart it. I am writing an application in Java which would benefit if I could update it on the fly without having to restart it. I had the idea to have a second jar that I could run when updated, which would be able to do the same thing that the IDE's do, but I have no clue how they swap the code. Can anyone enlighten me? Thank you!

like image 866
engineAL Avatar asked Oct 01 '22 06:10

engineAL


1 Answers

IDE's such as Eclipse, when debugging use the JDI (Java debug interface). It is a collection of Java API's which allow you to create / attach onto external Java Virtual Machines. This can be used in conjunction with a Java Agent which can be used to re-define classes in the virtual machine. What Eclipse does during debug mode is create a new Virtual Machine with your code running in it. When you make an edit and re-compile; eclipse then tells the JVM to re-define the classes you have changed, by sending over the new byte-code.

You can read the JDI documentation, and take a look at the Java instrument class (which is used to configure a class-transformer) in particular. Here's a related question on setting up a JDI launcher (launch a new JVM).

like image 151
T-Fowl Avatar answered Oct 06 '22 00:10

T-Fowl