从Windows批处理文件中自删除jar [英] Self Delete jar from Windows batch file

查看:183
本文介绍了从Windows批处理文件中自删除jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,应该通过在线下载新版本来进行自我更新,然后应该自我删除,我已经管理了更新,现在需要删除旧的jar文件. 由于我的应用程序只能在Windows上运行,因此我可以使用bat文件,因此我编写了用于创建应关闭JVM(因此jar文件不会被锁定)的批处理文件的代码,然后应删除该文件.然后将jar文件删除bat文件(bat文件应该能够自行删除). 代码是这样的:

I have a Java application that should update itself by downloading a new version online, and then it should self-delete itself, i already managed the update, now i need to delete the old jar file. Since my app will be running only on windows i thougt that i could use a bat file, so i wrote the code for creating a batch file that should close the JVM (so the jar file won't be locked) then it should delete the jar file and then it should delete the bat file (bat file should be able to self-delete). The code is this:

String pat = new File("delete.bat").getAbsolutePath();
FileWriter fw = new FileWriter(pat);
PrintWriter pw = new PrintWriter(fw);
pw.println("taskkill /F /IM \"java.exe\"");
pw.println("DEL /F \""+ jarname +"\"");
pw.println("DEL /F \"delete.bat\"");
pw.close();
System.out.println("cmd /c START \"\" \"" + pat + "\"");
Runtime.getRuntime().exec("cmd /c START \"\" \"" + pat + "\"");

实际上效果还不错,它删除了罐子和蝙蝠,但是...删除后仍然打开了命令提示符,看到它真的很丑:/ 我什至尝试将退出放在DEL/F"delete.bat"之后,但是由于bat已被删除,因此它无法读取退出命令... 关于如何关闭提示的一些想法?

That actually work not too bad, it delete the jar and the bat but... after deleting there is still the Command Prompt Opened, and that's really ugly to see :/ I even tryed putting exit after DEL /F "delete.bat", but since the bat is already deleted it cannot read the exit command... Some ideas on how could i make the prompt close?

推荐答案

我用Desktop.getDesktop().open(new File(pat));

I solved with Desktop.getDesktop().open(new File(pat));

希望这可以帮助某人.

这篇关于从Windows批处理文件中自删除jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆