title: How do I invoke a Java process from Windows Powershell?
body: <p>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.</p>
  
  <p>My test class is: </p>
  
  <pre><code>class HelloWorldApp { 
      public static void main(String[] args) { 
          System.out.println("Hello World!"); //Display the string. 
      } 
  } 
  </code></pre>
  
  <p>My PowerShell 2.0 code is this: </p>
  
  <pre><code>set-item -path Env:CLASSPATH -value C:\Test 
  "CLASSPATH = $Env:CLASSPATH" 
  [Diagnostics.Process]::Start('java.exe','-classpath $Env:CLASSPATH C:\
  Test\HelloWorldApp') 
  </code></pre>
  
  <p>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  : </p>
  
  <pre><code>java.exe -classpath $Env:CLASSPATH C:\Test\HelloWorldApp 
  </code></pre>
  
  <p>It causes an error.  I get this error: </p>
  
  <pre><code>PS &gt;C:\Test\Test.ps1 
  CLASSPATH = C:\Test 
  java.exe : java.lang.NoClassDefFoundError: C:\Test\HelloWorldApp 
  At C:\Test\Site.ps1:3 char:5 
  + java &lt;&lt;&lt;&lt;  -classpath $Env:CLASSPATH C:\Test\HelloWorldApp 
  + CategoryInfo : NotSpecified: (java.lang.NoCla...e\HelloWorldApp: 
                                 String) [], RemoteException 
  + FullyQualifiedErrorId : NativeCommandError 
  Exception in thread "main" 
  </code></pre>
  
  <p>As far as I can tell, my args are correct because here is what the 
  PCEX ( <a href="http://pscx.codeplex.com" rel="nofollow noreferrer">http://pscx.codeplex.com</a> ) echoargs cmdlet tells me: </p>
  
  <pre><code>PS &gt;echoargs java.exe -classpath $Env:CLASSPATH C:\Test\HelloWorldApp 
  Arg 0 is &lt;java.exe&gt; 
  Arg 1 is &lt;-classpath&gt; 
  Arg 2 is &lt;C:\Test&gt; 
  Arg 3 is &lt;C:\Test\HelloWorldApp&gt; 
  </code></pre>
  
  <p>Im convinced there is a way to get this to work because this code works:</p>
  
  <pre><code>## Test.ps1
  cd C:\PSJustice
  java.exe -classpath . HelloWorldApp
  </code></pre>
  
  <p>Also, this works:</p>
  
  <pre><code>cd C:\
  java.exe -classpath C:\Test HelloWorldApp
  </code></pre>