java - Runtime.getRuntime().exec how to run goto commands -
i'm trying run small command-line code in java application delete itself. command keep running , keeps trying delete file, , once application closed deleted.
i've tired passing command runtime.getruntime().exec still unable have work.
eg:
set file="program.jar" goto :d :r timeout /t 1 :d del %file% if exist %file% goto :r tried doing looks wrong. i've tired using ; instead of && 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 in .bat file how implement java .exec() method. run .bat file want code contained inside java.
first, can't use labels gotos in cmd one-liner. use kind of while true loop instead. in cmd terms: for /l %a in (1 0 2) ...
second, need apply delayed expansion. proof:
==> set "_file=init.ext" ==> set "_file=prog.jar" & /l %a in (1 1 2) @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% , %time% variables expanded in parse time.
on other side, delayed expansion enabled: !_file! , !time! variables expanded in execution time:
==> set "_file=init.ext" ==> cmd /e /v /c set "_file=prog.jar" ^& /l %a in (1 1 2) @echo !_file! !time! ^& timeout /t 3 1^>nul prog.jar 8:08:55,42 prog.jar 8:08:58,18 hence, one-liner follows (verified windows cmd cli locked file; loops until unlocked):
cmd /e /v /c set "_file=program.jar" ^& /l %a in (1 0 2) if exist "!_file!" (del "!_file!" ^& timeout /t 2) else (exit /b)
Comments
Post a Comment