How to call Java process from Windows Powershell?

I am having problems running a Java program with Windows Powershell 2.0. Any help on this would be greatly appreciated. I want the line "Hello world!" for printing on the main window of the Powershell console. Instead, its receipt is printed in a separate process window, which opens and then suddenly closes. I don't know exactly how to say powershell to redirect the stdout of the generated java process to the current powershell console. Basically, I want the behavior to be similar to what I get when java starts up under the DOS shell.

My test class:

class HelloWorldApp { 
    public static void main(String[] args) { 
        System.out.println("Hello World!"); //Display the string. 
    } 
} 

My PowerShell 2.0 code:

set-item -path Env:CLASSPATH -value C:\Test 
"CLASSPATH = $Env:CLASSPATH" 
[Diagnostics.Process]::Start('java.exe','-classpath $Env:CLASSPATH C:\
Test\HelloWorldApp') 

As an alternative, I tried to run it in the same way as with regular DOS, hoping that the output appears in the same console:

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 :

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 , , :

## Test.ps1
cd C:\PSJustice
java.exe -classpath . HelloWorldApp

, :

cd C:\
java.exe -classpath C:\Test HelloWorldApp
+3
1

, , . :

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

, . .

+5

Source: https://habr.com/ru/post/1711704/


All Articles