如何从 PowerShell 调用 Java 程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28691344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:38:26  来源:igfitidea点击:

How to call a Java program from PowerShell?

javawindowspowershell

提问by GibboK

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

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

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

But I need to have to run the app in a process (using Start-Process).

但是我需要在一个进程中运行该应用程序(使用Start-Process)。

I am trying the following with no sucess:

我正在尝试以下操作但没有成功:

Start-Process -FilePath java -jar $cls --js $dcn --js_output_file $dco -wait -windowstyle Normal

Error:

错误:

Start-Process : A parameter cannot be found that matches parameter name 'jar'.

Any idea how to fix it?

知道如何修复它吗?

采纳答案by abhijeet dhumal

You will need to use following format for powershell:

您将需要为 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:

Start-Job -ScriptBlock {
  & java -jar MyProgram.jar >console.out 2>console.err
}

回答by paxdiablo

It looks like the -jaris being picked up as an argument of Start-Processrather than being passed through to java.

看起来-jar是作为参数被拾取Start-Process而不是传递到java

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

尽管文档说明这-ArgumentList可选的,但我怀疑这并不适用于-option-type 的东西。

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):

例如,在 Powershell ISE 中,以下行会显示 Java 帮助(尽管很快消失了):

Start-Process -FilePath java -argumentlist -help

but thisline:

行:

Start-Process -FilePath java -help

causes Powershell itselfto complain about the -help.

导致 Powershell本身抱怨-help.