Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping a running jar at runtime

I am building an update system in which I need to be able to replace a referenced jar of a running application jar at runtime. However, I am running into file locking issues on Windows when trying to perform file utility functions on the jar such as 'setLastModified'.

After some googling I found this snippet...

What I found in my research is that the standard ClassLoader implementation never closes a jar file once it has been opened. It also only loads resources from the jar file as needed. So at any particular time, there may be Classes in the jar file that have not been loaded into memory. Obviously, if you delete the jar file out from under the ClassLoader, and it tries to find a resource in the missing file, you're going to get an IOException at the very least.

Does anyone have any references to information on doing this, or working around this issue?

Thanks.

like image 462
cweston Avatar asked Sep 01 '10 19:09

cweston


People also ask

How do I replace a .JAR file?

If graphical apps are an option, you could use winrar or 7-zip to replace the class. You don't need to extract the jar file to make this work. Just open the jar with one of those apps, go to de directory where is the class file to be replaced, drag-and-drop the new file to replace the old one and save.

How do I open a JAR file in Java Runtime Environment?

Right-click on the JAR file. Go to “Open With Other Applications”. Select Show other applications. Select Open With OpenJDK Java X Runtime.

Can I change code in JAR file?

You can use the JAR file editor to view or edit JAR file information that is stored in a data development project. If you make changes to JAR file information using the editor, you must deploy the JAR file to the database server again to apply the changes to the server version of the JAR file.


2 Answers

There is a lot of solutions for classes/lib hot-swapping :

  • Hotswap for ant
  • JRebel
  • OSGi

It depends on what you want to do.

like image 174
Colin Hebert Avatar answered Oct 26 '22 22:10

Colin Hebert


The closest thing is OSGi

Please have a look at the 'See also' section of the mentioned artice for concrete products.

like image 30
stacker Avatar answered Oct 27 '22 00:10

stacker