3

I have created an application, in which based on an operation, it modifies the properties file inside the jar.

I'm able to create a new temp jar with the updated changes. But can't rename the jar file to the existing jar file name. Since it is running, I am not able to delete and rename it.

Can any one suggest any operations or suggestions to delete the jar (Currently Executing) and rename the temp jar to that name?

I have doing for the Application: http://java.net/projects_tags/jeazyprops

sheidaei
  • 9,842
  • 20
  • 63
  • 86
  • Are you able to change the application to load the properties file from a different location, or does it have to be inside the JAR? – noahlz Oct 05 '12 at 17:36
  • I able to change it when it is in different Location. no issues on that... But i have to extend my project for the properties file inside the jar to... – Prabhu Prabhakaran Oct 08 '12 at 03:55

3 Answers3

3

You're problem is the default ClassLoader that pre-java 1.7 uses - it locks any jar that it loads and doesn't release the jar until execution completes. The easiest solution is to use Java 1.7 which should solve this problem. Otherwise you can write your own custom ClassLoader (ugh). Default ClassLoader: http://docs.oracle.com/javase/6/docs/api/java/net/URLClassLoader.html

EDIT

And here's the note from Oracle saying they fixed it in Java 1.7: http://openjdk.java.net/projects/jdk7/features/#f584

Nick Rippe
  • 6,465
  • 14
  • 30
2

I dont see a possibility of replacing the jar that you are executing from within the program.
Suggested Approach.
Create a new jar with a simple class that just launches your existing application. Before tha launch it should check if the temp.jar exists and if exists delete original and rename the temp.jar to original. In your application after creating the temp.jar, lauch the launcher class using Runtime.exec and exit. Your app will restart with new jar.

basiljames
  • 4,777
  • 4
  • 24
  • 41
1

May be you would need to write a custom class loader(extending ClassLoader) which enables you to load/unload the jar. You should be able to delete the jar if you can unload the jar.

Useful links http://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html

Can I dynamically unload and reload (other versions of the same) JAR?

Community
  • 1
  • 1
Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46