Discussion:
Running a Java program from PowerShell?
(too old to reply)
djangofan
2009-07-01 18:44:00 UTC
Permalink
I am having trouble running a Java programe with Windows Powershell.
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.

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, like I would with a regular
DOS shell so 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>
unknown
2009-07-25 10:22:09 UTC
Permalink
Just noticed no one had responded to this problem; sorry for the lag. ;)

In your first example, the problem is that when you use
[System.Diagnostics.Process]::Start(), by default it will create the process
in a new window.

Try creating a [System.Diagnostics.ProcessStartInfo] structure and set its
CreateNoWindow property to $true (you may also need to set UseShellExecute
to $false). After populating the application and arguments elements of the
ProcessStartInfo object, use _that_ as the argument for Start().

I believe the following should work, although I don't have a Java compiler
handy to allow me to test this:

$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.CreateNoWindow = $true
$psi.UseShellExecute = $false
$psi.FileName = 'java.exe'
$psi.Arguments = @("-classpath","$Env:CLASSPATH","C:\Test\HelloWorldApp")
[System.Diagnostics.Process]::Start($psi)
Post by djangofan
I am having trouble running a Java programe with Windows Powershell.
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.
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
set-item -path Env:CLASSPATH -value C:\Test
"CLASSPATH = $Env:CLASSPATH"
\Test\HelloWorldApp')
Alternatively, I tried to run it like so, like I would with a regular
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
String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Exception in thread "main"
As far as I can tell, my args are correct because here is what the
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>
Continue reading on narkive:
Loading...