0

I used to run a jar in background with below syntax on a linux machine as below :

java -jar ./target/myjar-0.0.1-SNAPSHOT.jar > /dev/null 2>&1

But now i have a requirement to run the same jar in background on the Powershell on a windows machine . (I am not using Powershell Core version which allows use of & keyword for running background jobs , my version is 5.1)

I tried running my jar in background as below based on this answer here on StackOverflow (https://stackoverflow.com/a/25035181/4193280):

Start-Job -ScriptBlock {
    & java -jar ./target/myjar-0.0.1-SNAPSHOT.jar >console.out 2>console.err
}

But still the jar is not executing and i am not able to connect on my application on port 9090 (that is where i have configured it to run) .

Please let me know and help where i am missing the point so that i can execute the jar in background . Thank you .

Saurabh Chaturvedi
  • 2,028
  • 2
  • 18
  • 39
  • 1
    Do you want the Java application keep running after your PowerShell script terminates? Then you need to use `Start-Process`. – zett42 Mar 18 '21 at 17:31
  • Yes , thats the requirement @zett42. I will try with that . Will that still run in background ? – Saurabh Chaturvedi Mar 18 '21 at 17:43
  • 1
    Try using the full path of your jar file for starters, whats the error when you receive the job? – Santiago Squarzon Mar 18 '21 at 17:59
  • @santisq I tried running the jar without a background process its running prooperly :Like this java -jar ./target/myjar-0.0.1-SNAPSHOT.jar >console.out 2>console.err . So i dont think any issue on full path . Can you paste the exact script syntax that you are suggesting ? – Saurabh Chaturvedi Mar 18 '21 at 18:02
  • getting this error .. + CategoryInfo : InvalidArgument: (:) [Start-Job], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.StartJobCommand – Saurabh Chaturvedi Mar 18 '21 at 18:03
  • using command as - Start-Job java -jar ./target/myjar-0.0.1-SNAPSHOT.jar >console.out 2>console.err – Saurabh Chaturvedi Mar 18 '21 at 18:04
  • 1
    What I mean is, Jobs have their own scope and their own scoped path, your Job's path is not the same as your script path. Hence I suggested using the full path of your jar file instead of ./path – Santiago Squarzon Mar 18 '21 at 18:12
  • 1
    Yes, using `Start-Process` without parameter `-wait` lets the process running in the background, even when the PS script terminates. – zett42 Mar 18 '21 at 18:23

0 Answers0