如何从PowerShell调用Java程序?

Gib*_*boK 7 java windows powershell

我需要从PowerShell调用java程序(jar文件).以下代码有效:

java -jar $cls --js $dcn --js_output_file $dco
Run Code Online (Sandbox Code Playgroud)

但我需要在一个过程中运行应用程序(使用Start-Process).

我没有成功地尝试以下内容:

Start-Process -FilePath java -jar $cls --js $dcn --js_output_file $dco -wait -windowstyle Normal
Run Code Online (Sandbox Code Playgroud)

错误:

Start-Process : A parameter cannot be found that matches parameter name 'jar'.
Run Code Online (Sandbox Code Playgroud)

知道怎么解决吗?

abh*_*mal 14

您需要使用powershell的以下格式:

 Start-Process java -ArgumentList '-jar', 'MyProgram.jar' `
-RedirectStandardOutput '.\console.out' -RedirectStandardError '.\console.err' 
Run Code Online (Sandbox Code Playgroud)

或者您可以使用的其他选项是Start-job:

Start-Job -ScriptBlock {
  & java -jar MyProgram.jar >console.out 2>console.err
}
Run Code Online (Sandbox Code Playgroud)