【发布时间】:2010-11-07 11:32:58
【问题描述】:
我在使用 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
【问题讨论】:
-
您收到 NoClassDefFoundError 是因为 java.exe 期望从类所在的目录(或包结构的根目录)调用。不过不知道 PowerShell 部分。
标签: java powershell stdout