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!
-
I'd consider reading [this](http://stackoverflow.com/questions/7989135/is-it-possible-to-programmatically-compile-java-source-code-in-memory-only). Either way in the accepted answer would probably suit your needs. – ChiefTwoPencils Jul 25 '14 at 04:40
-
1Take a look at JDWP, and specifically [JDI](http://docs.oracle.com/javase/7/docs/technotes/guides/jpda/architecture.html#jdi). – Elliott Frisch Jul 25 '14 at 04:41
2 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).
I was actually learning about the same thing @work today. I found this article pretty helpful in understanding the workflow behind a few of the more complicated hotpush cases:
http://java.dzone.com/articles/5-jrebel-features-you-couldn%E2%80%99t

- 5,984
- 3
- 19
- 21