How to call a Java program from PowerShell?

后端 未结 2 1123
我在风中等你
我在风中等你 2020-12-15 00:31

I need to call a java program (jar file )from PowerShell. The following code works:

java -jar $cls --js $dcn --js_output_file $dco

But I ne

相关标签:
2条回答
  • 2020-12-15 00:59

    It looks like the -jar is being picked up as an argument of Start-Process rather than being passed through to java.

    Although the documentation states that -ArgumentList is optional, I suspect that doesn't count for -option-type things.

    You probably need to use:

    Start-Process -FilePath java -ArgumentList ...
    

    For example, in Powershell ISE, the following line brings up the Java help (albeit quickly disappearing):

    Start-Process -FilePath java -argumentlist -help
    

    but this line:

    Start-Process -FilePath java -help
    

    causes Powershell itself to complain about the -help.

    0 讨论(0)
  • 2020-12-15 01:17

    You will need to use following format for powershell:

     Start-Process java -ArgumentList '-jar', 'MyProgram.jar' `
    -RedirectStandardOutput '.\console.out' -RedirectStandardError '.\console.err' 
    

    Or other option you can use is Start-job:

    Start-Job -ScriptBlock {
      & java -jar MyProgram.jar >console.out 2>console.err
    }
    
    0 讨论(0)
提交回复
热议问题