'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?



Solution 1:[1]

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"
}

Solution 2:[2]

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.

Solution 3:[3]

There is another problem with the code above.

$java="C:\Program Files (x86)\Minecraft Launcher\AdoptOpenJDK\jre-8.0.222.10-hotspot\bin\java.exe"
Start-Process -FilePath $java -ArgumentList '-jar "C:\Program Files (x86)\Minecraft Launcher\Mineshafter-launcher.jar"'

In this code I am using the java runtime that is bundled with the Minecraft Launcher to run Mineshafter.

When I tried it using the answer above, the script executed but nothing happened. This is why I put the path to the .jar file in double quotes. After I did that, the jar executed perfectly fine. Also, when defining the $java variable, paste the full path to the java.exe file, not just the folder. This helps because PowerShell will simply use that executable instead of searching for one. If there are multiple versions of Java installed in the same directory, PowerShell may pick the wrong one.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 FarIDM
Solution 3 Community