如何从 Windows Powershell 调用 Java 进程?
我在使用 Windows Powershell 2.0 运行 Java 程序时遇到问题。 对此的任何帮助将不胜感激。 我想要字符串 “你好世界!” 打印到主 Powershell 控制台窗口。 相反,它被打印到打开的单独进程窗口 然后突然关闭。 我不确切知道如何告诉 powershell 将生成的 java 进程的标准输出重定向到当前的 powershell 控制台。 基本上,我想要的行为就像在 DOS shell 下运行 java 时得到的那样。
我的测试类是:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
我的 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')
或者,我尝试像这样运行它,就像使用常规程序一样 DOS shell,希望输出显示在同一控制台中:
java.exe -classpath $Env:CLASSPATH C:\Test\HelloWorldApp
它会导致错误。 我收到此错误:
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"
据我所知,我的参数是正确的,因为这就是 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>
我相信有一种方法可以让它工作,因为这段代码有效:
## Test.ps1
cd C:\PSJustice
java.exe -classpath . HelloWorldApp
另外,这有效:
cd C:\
java.exe -classpath C:\Test HelloWorldApp
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.
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:
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 :
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于弄明白了。 这是最小的拼写错误:
指定类名时,不能包含类名前缀的绝对路径。 哎呀。
I finally figured it out. It was the smallest typo :
When specifying the Class name it cannot include the absolute path prefixing the class name. Oops.