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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…