如何从 Windows Powershell 调用 Java 进程?

发布于 2024-07-26 03:06:26 字数 1878 浏览 4 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自演自醉 2024-08-02 03:06:27

我终于弄明白了。 这是最小的拼写错误:

cd c:\
set-item -path Env:CLASSPATH -value C:\Test 
"CLASSPATH = $Env:CLASSPATH" 
java.exe -classpath $Env:CLASSPATH HelloWorldApp

指定类名时,不能包含类名前缀的绝对路径。 哎呀。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文