1

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 + "\"");

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?

Celeste Capece
  • 1,306
  • 2
  • 11
  • 20
  • Why not delete the jar from Java code? A lot cleaner (and multiplatform) solution than what you're trying to do. – Kayaman May 21 '15 at 08:39

2 Answers2

1

You can close the command prompt in two ways

  1. adding Exit at the last line of the bat script pw.println("exit");
  2. Launching CMD with start rather cmd

    Runtime.getRuntime().exec(" start \"\" \"" + pat + "\"");

Akash Yadav
  • 2,411
  • 20
  • 32
  • As i said i already tryed exit, but it doesen't work since the bat is deleted before the exit, and the cmd cannot read the exit, anyway i will try with the second way :) – Celeste Capece May 21 '15 at 08:47
  • I don't know why, but the bat file doesen't start this way – Celeste Capece May 21 '15 at 08:50
  • can you put the error you see when you are launching with start – Akash Yadav May 21 '15 at 08:51
  • I don't see nothing, there's no exception, it just doesen't start... I'll try to get the running string and start it from Win+R – Celeste Capece May 21 '15 at 09:46
  • i tryed to run the command start "" "C:\Users\Administrator\Desktop\MangaDownloader\delete.bat" by Win+R and it say Windows cannot find 'start' It seems like i cannot use the start command without using cmd /c – Celeste Capece May 21 '15 at 09:50
1

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

Hope this can help someone.

Celeste Capece
  • 1,306
  • 2
  • 11
  • 20