Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a Java program from PowerShell?

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?

like image 406
GibboK Avatar asked Feb 24 '15 08:02

GibboK


People also ask

How do you call a Java program?

To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon ( ; ). A class must have a matching filename ( Main and Main.java).

How do you call Java in terminal?

We need to use the command javac file_name_with_the_extension . For example, as I want to compile my Main. java , I will use the command javac Main. java .


2 Answers

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
}
like image 62
Abhijeet Dhumal Avatar answered Sep 30 '22 18:09

Abhijeet Dhumal


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.

like image 22
paxdiablo Avatar answered Sep 30 '22 19:09

paxdiablo