0

Is it a good idea to compile java classes in runtime for updating a project without stopping the server?

And my next question is: can I replace the class of an existing object? I mean update some method behavior without recreating the Object.

ElliotSchmelliot
  • 7,322
  • 4
  • 41
  • 64
Oleksandr Samsonov
  • 1,067
  • 2
  • 14
  • 29
  • 1
    You can do it with the debugging API, but it's not a good idea to do this in a production environment. – Joachim Sauer Sep 17 '13 at 09:30
  • 2
    A better idea would be to use a modular framework like OSGi, then you can replace parts of the system during runtime. – Kayaman Sep 17 '13 at 09:41

2 Answers2

1

Is it good idea to compile java classes in runtime for update project without stop the server?

I believe you mean that updating the newly compiled classes on a server without stoppping it. You can always do that but if the class is already loaded then it will make no difference and the jvm will keep using the older version of the class.

And next question is: can i replace the class of existing object? I mean update some method behavior without recreating Object.

Same answer as for the first question. If the class is already loaded then JVM will be using the old method/behavior. And if you have some object of the class that means it is already loaded.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • It is possible to unload classes, but it would still be a bad solution for this. – Kayaman Sep 17 '13 at 09:43
  • for first question I can create classLoader factory and load new classes in new class loader if it updated. But is it allowable developer method? – Oleksandr Samsonov Sep 17 '13 at 10:26
  • @ОлександрСамсонов I believe it is allowed and logical to do sometimes. I believe some containers such as Websphere provide hot deployment, which means the classes will be loaded once updated. – Juned Ahsan Sep 17 '13 at 10:34
1

I would make the redployment process so fast it doesn't matter. (This is not always an option) but I try to ensure I can compile, redeploy and restart a server process in well under 10 seconds. In this situation you don't need dynamic compilation.

Note: if you debugging a system and you recompile code, you can hot swap the changes for a running system from your IDE. (Usually works but sometimes does not depending on what you change)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130