How to call a Java program from PowerShell?

46,627

Solution 1

You will need to use following format for powershell:

 Start-Process java -ArgumentList '-jar', 'MyProgram.jar' `
-RedirectStandardOutput '.\console.out' -RedirectStandardError '.\console.err' 

Or other option you can use is Start-job:

Start-Job -ScriptBlock {
  & java -jar MyProgram.jar >console.out 2>console.err
}

Solution 2

It looks like the -jar is being picked up as an argument of Start-Process rather than being passed through to java.

Although the documentation states that -ArgumentList is optional, I suspect that doesn't count for -option-type things.

You probably need to use:

Start-Process -FilePath java -ArgumentList ...

For example, in Powershell ISE, the following line brings up the Java help (albeit quickly disappearing):

Start-Process -FilePath java -argumentlist -help

but this line:

Start-Process -FilePath java -help

causes Powershell itself to complain about the -help.

Share:
46,627
GibboK
Author by

GibboK

A professional and enthusiastic Senior Front End Developer. Listed as top 2 users by reputation in Czech Republic on Stack Overflow. Latest open source projects Animatelo - Porting to JavaScript Web Animations API of Animate.css (430+ stars on GitHub) Industrial UI - Simple, modular UI Components for Shop Floor Applications Frontend Boilerplate - An opinionated boilerplate which helps you build fast, robust, and adaptable single-page application in React Keyframes Tool - Command line tool which convert CSS Animations to JavaScript objects gibbok.coding📧gmail.com

Updated on February 24, 2021

Comments

  • GibboK
    GibboK about 3 years

    I need to call a java program (jar file )from PowerShell. The following code works:

    java -jar $cls --js $dcn --js_output_file $dco
    

    But I need to have to run the app in a process (using Start-Process).

    I am trying the following with no sucess:

    Start-Process -FilePath java -jar $cls --js $dcn --js_output_file $dco -wait -windowstyle Normal
    

    Error:

    Start-Process : A parameter cannot be found that matches parameter name 'jar'.
    

    Any idea how to fix it?