0

I am trying to execute a powershell script from my java console app, I was able to get this working with the below command:

Process p = Runtime.getRuntime().exec("cmd.exe /c start cd "+dir+" & start cmd.exe /k \"Powershell C:\\runscript.ps1 args\"",null,dir);
p.waitFor();

Inside my powershell script, i have the below snippet which gets called in a loop,

    Start-Process -FilePath $RunLocation -ArgumentList $args -wait -NoNewWindow -RedirectStandardOutput $OutputFile

$output = Get-Content $OutputFile| out-string   
    if(!($output.toLower().contains("failed"){
        Remove-Item $outLogFile
}

If I open command prompt, and copy exactly what I have in my exec(...) command, it runs great, however, when I run it in my Java application, it seems like the -wait in my powershell script is being ignored, and the next line (which is checking and removing logs) is run, I've even gone to the length of adding a sleep for a few seconds in my powershell just after the Start-Process, this works but I'm hoping there is a better way.

The error i am getting is in the powershell script is below (this only happens when ive run it from my Java app, the -wait waits foor the start process to finsih before continuing when run directly from command prompt..):

Remove-Item : Cannot remove item D:\adhoc\logs\2016-06\output38844448.out: The process cannot access the file
'D:\adhoc\logs\2016-06\output38844448.out' because it is being used by another process.
At C:\runscript.ps1:91 char:3
+         Remove-Item $OutputFile
+         ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (D:\adhoc\log...4448.out:FileInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

Why is the -wait in my powershell script not working when I run it from my Java app using runtime().exec?

Kelly b
  • 171
  • 2
  • 14
  • print that fired console script error so that you can get idea what is going wrong. Use this piece of code so that you can get error stream . InputStream stderr = p.getErrorStream(); InputStreamReader isr = new InputStreamReader(stderr); BufferedReader br = new BufferedReader(isr); while ( (line = br.readLine()) != null) logger.info(line); print that error so that we can give you proper solution regarding it. – Darshit Jun 27 '16 at 09:43
  • Added more info to original question – Kelly b Jun 27 '16 at 11:57
  • See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. – Andrew Thompson Jun 28 '16 at 05:58
  • Not sure what the cause is, but a better workaround was to modify the Powershell script, I found a previous post with a near indentical issue... http://stackoverflow.com/questions/20790798/start-process-wait-doesnt-work-when-script-is-launched-from-command-prompt-ope – Kelly b Jul 05 '16 at 04:25

0 Answers0