如何从 Windows Powershell 调用 Java 进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1071602/
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
How do I invoke a Java process from Windows Powershell?
提问by djangofan
I am having trouble running a Java program with Windows Powershell 2.0. Any help on this would be greatly appreciated. I want the string "Hello World!" to print to the main Powershell console window. Instead, its getting printed to a separate process window that opens then suddenly closes. I don't know exactly how to tell the powershell to redirect the stdout of the spawned java process to the current powershell console. Basically, i want behavior just like what I get when running java under a DOS shell.
我在使用 Windows Powershell 2.0 运行 Java 程序时遇到问题。对此的任何帮助将不胜感激。我想要字符串“Hello World!” 打印到主 Powershell 控制台窗口。相反,它被打印到一个单独的进程窗口,该窗口打开然后突然关闭。我不知道如何告诉 powershell 将生成的 java 进程的标准输出重定向到当前的 powershell 控制台。基本上,我想要的行为就像我在 DOS shell 下运行 java 时得到的一样。
My test class is:
我的测试课是:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
My PowerShell 2.0 code is this:
我的 PowerShell 2.0 代码是这样的:
set-item -path Env:CLASSPATH -value C:\Test
"CLASSPATH = $Env:CLASSPATH"
[Diagnostics.Process]::Start('java.exe','-classpath $Env:CLASSPATH C:\
Test\HelloWorldApp')
Alternatively, I tried to run it like so, as I would with a regular DOS shell, with the hope that the output shows in the same console :
或者,我尝试像使用常规 DOS shell 一样运行它,希望输出显示在同一个控制台中:
java.exe -classpath $Env:CLASSPATH C:\Test\HelloWorldApp
It causes an error. I get this error:
它会导致错误。我收到此错误:
PS >C:\Test\Test.ps1
CLASSPATH = C:\Test
java.exe : java.lang.NoClassDefFoundError: C:\Test\HelloWorldApp
At C:\Test\Site.ps1:3 char:5
+ java <<<< -classpath $Env:CLASSPATH C:\Test\HelloWorldApp
+ CategoryInfo : NotSpecified: (java.lang.NoCla...e\HelloWorldApp:
String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Exception in thread "main"
As far as I can tell, my args are correct because here is what the PCEX ( http://pscx.codeplex.com) echoargs cmdlet tells me:
据我所知,我的参数是正确的,因为这里是 PCEX ( http://pscx.codeplex.com) echoargs cmdlet 告诉我的:
PS >echoargs java.exe -classpath $Env:CLASSPATH C:\Test\HelloWorldApp
Arg 0 is <java.exe>
Arg 1 is <-classpath>
Arg 2 is <C:\Test>
Arg 3 is <C:\Test\HelloWorldApp>
Im convinced there is a way to get this to work because this code works:
我相信有一种方法可以使它起作用,因为此代码有效:
## Test.ps1
cd C:\PSJustice
java.exe -classpath . HelloWorldApp
Also, this works:
此外,这有效:
cd C:\
java.exe -classpath C:\Test HelloWorldApp
回答by djangofan
I finally figured it out. It was the smallest typo :
我终于弄明白了。这是最小的错字:
cd c:\
set-item -path Env:CLASSPATH -value C:\Test
"CLASSPATH = $Env:CLASSPATH"
java.exe -classpath $Env:CLASSPATH HelloWorldApp
When specifying the Class name it cannot include the absolute path prefixing the class name. Oops.
指定类名时,不能在类名前包含绝对路径。哎呀。

