Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

How do I delete executable JAR after running it inside Java program?

I am using a Java program to download JARs from a private online database, and then run them using the following code:

ProcessBuilder pb = new ProcessBuilder(commands);
File jarFile = new File(JAR_DIR);
pb.directory(jarFile);
Process proc = pb.start();

The JARs run fine using this, but I would like to delete each JAR after using it because there are a ton of them and it would require too much space to just continue downloading them.

I have been printing out each line in the JAR and then waiting for it to close (I have also tried destroying the process after waiting for it to close):

BufferedReader r = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line;
while(line = r.readLine()) != null)
    System.out.println(line);
proc.waitFor();

Then I delete the folder where the JAR is located using a method from org.apache.commons.io.FileUtils:

FileUtils.deleteDirectory(new File(jarDirectory));

However, this throws an Exception saying the JAR file can't be deleted.

I think there's somewhere I need to remove the JAR's path from the JVM before deleting it, but I haven't found anything on how to do this inside another Java Program.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...