Akki
Akki

Reputation: 171

Run a jar file from powershell

I want to run a jar file from powershell. Till now I have been able to find this:

Start-Process -FilePath java -ArgumentList '-jar Upload_Download.jar FilePickupPath= $pickuppath FileDownloadPath= $download' -PassThru -RedirectStandardError E:\stderr.txt 

Some how this is not working. Any suggestions?

Upvotes: 4

Views: 44065

Answers (4)

amba 16
amba 16

Reputation: 1

When you run the command after defining the $java variable, make sure to put the full path of the jar in double quotes inside the single quotes. This is to fix the error that occurs when the path of the file has a space in it.

The other option is to cd into the folder with the jar and run the command that way, as suggested in the above answer. This also works and is helpful with simple paths.

Also when defining the java variable, try putting the path to the executable instead of just the folder. This prevents execution conflicts if paths are similar.

Upvotes: 0

Chris Mills
Chris Mills

Reputation: 410

Passing the arguments is often a problem so I use an array,

also avoid long lines by using parameter splatting.

if(-not $env:JAVA_HOME)
{
    Write-Error "JAVA_HOME not set"
    break
}

$params = @{
    FilePath = [string]::Format("{0}\bin\java.exe",$env:JAVA_HOME)
    WorkingDirectory = "D:\SDL\Web\live\discovery\config\"
    ArgumentList = @("-jar", "discovery-registration.jar", "update")
    RedirectStandardError = "c:\temp\JavaError.txt"
    PassThru = $true
    Wait = $true
}

$p = Start-Process @params

if($p.ExitCode -eq 0)
{
    Write-Output "Discovery Registration complete"
}
else
{
    Write-Output "Discovery Registration failed"
}

Upvotes: 5

FarIDM
FarIDM

Reputation: 129

My problem was a little different because there are two jre versions and one jdk installed and powershell was always picking up jre based java executable and failing with the message

C:\Program Files\Java\jre1.8.0_73 is not a valid JDK Java Home.

Although jdk was installed and JAVA_HOME variable was set and out of variable displays the jdk path correctly.

> $env:JAVA_HOME
C:\Program Files\Java\jdk1.8.0_73

I stored the jdk java executable path in a variable, navigated to the directory containing jar file for Web logic server and invoked the installation program using following code.

 $java="C:\Program Files\Java\jdk1.8.0_73\bin\java"
 Start-Process -FilePath $java -ArgumentList '-jar fmw_12.2.1.3.0_infrastructure_generic.jar'

The installation program opened after few seconds. I have omitted -RedirectStandardErro switch to keep the command small and simple although i did use it to catch any error in the first place.

I tried using variable $fmw to store the jar file fmw_12.2.1.3.0_infrastructure_generic.jar but it did not work. Unsuccessful code is as follows

$fmw="fmw_12.2.1.3.0_infrastructure_generic.jar"
Start-Process -FilePath $java -ArgumentList '-jar $fmw'

I hope this would help someone and somebody might provide me a better and clean method to call jar files in powershell.

Upvotes: 0

vonPryz
vonPryz

Reputation: 24071

Powershell has multiple string quotation characters that behave different ways. The double quote " allows evaluations within the string whilst the single quote ' doesn't.

As a practical example:

$foo=42
write-host "The foo: $foo"
# Prints The foo: 42
write-host 'The foo: $foo'
# Prints The foo: $foo

The command uses single quote like so, (backtick is used to split the code into screen friendly format)

Start-Process -FilePath java `
-ArgumentList '-jar Upload_Download.jar FilePickupPath= $pickuppath fileDownloadPath= $download' `
-PassThru -RedirectStandardError E:\stderr.txt 

This will pass literally $pickuppath and $download. The intention is likely to pass $pickuppath and $download's values.

In order to resolve the issue, use double quotes.

Upvotes: 10

Related Questions