Search code examples
javapowershellbatch-fileantjasperserver

Problems installing Jasperserver with Powershell. ANT can't see Java


I'm trying to install Jasperserver onto a Windows VM with Powershell.

I can install Java just fine, but some subsequent bat files that get run are complaining they can't find environment variables such as JAVA_HOME. I can see they exist however.

I add the environment variables:

[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Java\jdk1.8.0_91", "Machine")
[Environment]::SetEnvironmentVariable("JRE_HOME", "C:\Program Files\Java\jre1.8.0_91", "Machine")
[Environment]::SetEnvironmentVariable("CLASSPATH", "C:\Program Files\Java\jdk1.8.0_91\jre1.8.0_91\lib\rt.jar", "Machine")

And also the path variable. Going into System -> Advanced System Settings -> Environment variables I can see they are there, so I assume it's not just setting the powershell session.

When I try to run ANT (though a build in batch)

cd C:\Jaspersoft\jasperreports-server-6.2.0\apache-tomcat\bin
$arguments = '/c service.bat install' Start-Process cmd -Wait -PassThru -ArgumentList $arguments

it gives me this error:

> WARNING: JAVA_HOME environment variable not found  [minimal]  Running
> install-minimal-pro Ant task 
> ----------------------------------------------------------------------  '"java.exe"' is not recognized as an internal or external command,
> operable program or batch file. Checking Ant return code: OK

I've tried:

  • Setting the environments several ways (setx.exe, etc)
  • Running the bat in many ways, start-process, cmd, in an invoke-command, etc

If I reboot the server it will install fine. Also, if I run the batch directly in a command prompt it's fine.

Before I have to use an image with Java already installed, I want to understand why this is happening and ideally make it work.


Solution

  • Both SetEnvironmentVariable(..., "Machine") and setx set the variable in the registry. This value is used for future processes launched by explorer (such as after a reboot), but it is NOT set for the current process or its children. Your script needs to set the variables for the current process.

    Using the .NET syntax:

    [Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Java\jdk1.8.0_91", "Process")
    

    Or, using PS syntax

    $env:JAVA_HOME = "C:\Program Files\Java\jdk1.8.0_91"