简体   繁体   English

如何从Windows Powershell调用Java进程?

[英]How do I invoke a Java process from Windows Powershell?

I am having trouble running a Java program with Windows Powershell 2.0. 我在使用Windows Powershell 2.0运行Java程序时遇到问题。 Any help on this would be greatly appreciated. 任何有关这方面的帮助将不胜感激。 I want the string "Hello World!" 我想要字符串“Hello World!” to print to the main Powershell console window. 打印到Powershell主控制台窗口。 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. 我不知道如何告诉powershell将生成的java进程的stdout重定向到当前的PowerShell控制台。 Basically, i want behavior just like what I get when running java under a DOS shell. 基本上,我想要的行为就像我在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: 据我所知,我的args是正确的,因为这是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

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. 哎呀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM