简体   繁体   English

如何删除正在执行的jar文件

[英]How to delete a executing jar file

I have created an application, in which based on an operation, it modifies the properties file inside the jar. 我创建了一个应用程序,在其中基于操作,它修改jar中的属性文件。

I'm able to create a new temp jar with the updated changes. 我能够使用更新的更改创建一个新的临时jar。 But can't rename the jar file to the existing jar file name. 但是无法将jar文件重命名为现有的jar文件名。 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? 任何人都可以建议删除jar(当前正在执行)的任何操作或建议,并将temp jar重命名为该名称吗?

I have doing for the Application: http://java.net/projects_tags/jeazyprops 我已经为应用程序做了: http//java.net/projects_tags/jeazyprops

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. 您遇到的问题是java 1.7之前使用的默认ClassLoader - 它会锁定它加载的任何jar,并且在执行完成之前不会释放jar。 The easiest solution is to use Java 1.7 which should solve this problem. 最简单的解决方案是使用Java 1.7来解决这个问题。 Otherwise you can write your own custom ClassLoader (ugh). 否则你可以编写自己的自定义ClassLoader (呃)。 Default ClassLoader : http://docs.oracle.com/javase/6/docs/api/java/net/URLClassLoader.html 默认ClassLoaderhttp//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 以下是Oracle的说明,他们在Java 1.7中修复了它: http//openjdk.java.net/projects/jdk7/features/#f584

May be you would need to write a custom class loader(extending ClassLoader) which enables you to load/unload the jar. 可能你需要编写一个自定义类加载器(扩展ClassLoader),它允许你加载/卸载jar。 You should be able to delete the jar if you can unload the jar. 如果你可以卸载jar,你应该能够删除jar。

Useful links http://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html 有用的链接http://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html

Can I dynamically unload and reload (other versions of the same) JAR? 我可以动态卸载和重新加载(相同的其他版本)JAR吗?

I dont see a possibility of replacing the jar that you are executing from within the program. 我没有看到在程序中替换正在执行的jar的可能性。
Suggested Approach. 建议的方法。
Create a new jar with a simple class that just launches your existing application. 使用只启动现有应用程序的简单类创建一个新jar。 Before tha launch it should check if the temp.jar exists and if exists delete original and rename the temp.jar to original. 在启动之前,它应该检查temp.jar存在以及是否存在delete original并将temp.jar重命名为original。 In your application after creating the temp.jar , lauch the launcher class using Runtime.exec and exit. 在创建temp.jar后的应用程序中,使用Runtime.exec启动launcher类并退出。 Your app will restart with new jar. 您的应用将使用新jar启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM