10

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?

1
  • Knowing what the actual problem is, I would say that would be a nice thing to have in order to be able to fix it. "No success" is hardly the most verbose problem report I've ever seen :-) Commented Feb 24, 2015 at 8:56

3 Answers 3

21

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer, could you please provide me an example using my code, I have problem in setting argumentlist
You need to add your jar name there and if any arguments.
sorry i get an error in ArgumentList, could you please provide me an example with multiple arguments to pass.....in my case are variable like $dcn and so on... thanks
You will need to quote the parameter. like "$dcn" while passing it to command.
1

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.

1 Comment

thanks for your answer, could you please provide me an example using my code, I have problem in setting argumentlist
0

Option 1 [Using Start-Job ScriptBlock]

Start-Job -ScriptBlock {
  & java -cp .\Runner.jar com.abc.bcd.Runner.java >console.out 2>console.err
}
if ( $? == "True")
   write-host("Agent started successfully")
else if ($? == "False")
   write-host("Agent did not start")

Option 2 [Using Start-Process]

Start-Process -FilePath '.\jre\bin\java' -WindowStyle Hidden -Wait -ArgumentList "-cp .\Runner.jar com.abc.bcd.Runner"

That's how i did it using above two options initially.

Option 3 [Using apache-commons-daemon]

I can suggest a better and robust alternative.

You can use apache-commons-daemon library to build a windows service for your java application and then start, stop the service very conveniently.

There is amazing youtube video which will explain apache commons daemon and how to build a windows service. I will attach the reference at the end.

References :

https://commons.apache.org/proper/commons-daemon/index.html

https://www.youtube.com/watch?v=7NjdTlriM1g

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.