1

I'm trying to run small command-line code in java application to delete itself. So the command would keep running and keeps trying to delete the file, and once the application is closed it would be deleted.

I've tired passing the command into Runtime.getRuntime().exec but still unable to have it work.

eg:

SET file="program.jar"
goto :d
:r
timeout /t 1
:d
del %file%
if exist %file% goto :r

Tried doing this which obviously looks wrong. I've tired using ; instead of && but doesn't work either.

Runtime.getRuntime().exec("cmd /c \"SET file=\"program.jar\" && goto :d && :r && timeout /t 1 && :d && del %file% && if exist %file% goto :r\"");

Works perfectly well in a .bat file but how do i implement it into java .exec() method. I could just run the .bat file but I would want all the code be contained inside java.

Tim
  • 306
  • 2
  • 10
  • 1
    If the script is correct, then this is probably an access right problem. Why don' you delete the file using java code ? – Dici Sep 30 '15 at 17:50
  • I know the script is correct, but I've the problem in passing the script correctly into the .exec() method. Updated the question on that. – Tim Sep 30 '15 at 17:52
  • Are you sure `&&` is valid for the Windows cmd ? – Dici Sep 30 '15 at 17:56
  • Works ok with `Runtime.getRuntime().exec("cmd /c \"notepad && notepad\"");` though it would open 1 notepad, and if i close the notepad then the second notepad would start. I'm sure I'm just passing in the command-line in a wrong manner but i don't know how . – Tim Sep 30 '15 at 18:02
  • 1
    I recomend you use a `ProcessBuilder` instead and read its errorStream – Dici Sep 30 '15 at 18:11
  • Tired so, no error produced. Attempting to run notepad still works, running an invalid application e.g notepa gives the 'notepa is not recognized error ...' – Tim Sep 30 '15 at 18:40
  • Have you seen this discussion? http://stackoverflow.com/questions/355988/how-do-i-deal-with-quote-characters-when-using-cmd-exe – rsutormin Sep 30 '15 at 18:57
  • 2
    Second question is what happens if you move all commands with && into separate temp.bat file and run it through `Runtime.getRuntime().exec("cmd /c temp.bat");` ? – rsutormin Sep 30 '15 at 19:01

1 Answers1

1

First, you can't use labels an gotos in a cmd one-liner. Use some kind of while TRUE loop instead. In cmd terms: for /L %a in (1 0 2) do ...

Second, you need to apply Delayed Expansion. Proof:

==> SET "_file=init.ext"

==> SET "_file=prog.jar" & for /L %a in (1 1 2) do @echo %_file% %time% & timeout /T 3 1>NUL
init.ext  8:05:55,33
init.ext  8:05:55,33

==> set _file
_file=prog.jar

In above example, %_file% and %time% variables are expanded in parse time.
On the other side, with delayed expansion enabled: !_file! and !time! variables are expanded in execution time:

==> SET "_file=init.ext"

==> cmd /E /V /C SET "_file=prog.jar" ^& for /L %a in (1 1 2) do @echo !_file! !time! ^& timeout /T 3 1^>NUL
prog.jar  8:08:55,42
prog.jar  8:08:58,18

Hence, your one-liner could look as follows (verified from Windows cmd CLI for a locked file; loops until unlocked):

cmd /E /V /C SET "_file=program.jar" ^& for /L %a in (1 0 2) do if exist "!_file!" (del "!_file!" ^& timeout /T 2) else (exit /B)
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • I've tired that one liner in cmd and it works perfectly but apparently it does nothing when using .exec(...); – Tim Oct 01 '15 at 11:25
  • Sorry, `java` is not my coffee cup:) Perhaps some characters shoud be properly escaped? In a different way than as used in pure `cmd` CLI? – JosefZ Oct 01 '15 at 14:26
  • I'll try to work on it. Thanks anyways! at least i got something to work with now. – Tim Oct 02 '15 at 13:13