forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Use-Java.ps1
39 lines (35 loc) · 1.37 KB
/
Use-Java.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<#
.SYNOPSIS
Switch the Java version for the current process by modifying environment variables.
.INPUTS
System.String path to use as the new JAVA_HOME environment variable.
.EXAMPLE
Use-Java.ps1 "$env:ProgramFiles\OpenJDK\jdk-11.0.1"
#>
[CmdletBinding()][OutputType([void])] Param(
# The path to the JRE/JDK to use, which must contain bin\java.exe.
[Parameter(Position=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[ValidateScript({(Test-Path $_ -PathType Container) -and (Test-Path "$_\bin\java.exe" -PathType Leaf)})]
[Alias('FullName')][string] $Path
)
$env:JAVA_HOME = $Path
Write-Verbose "JAVA_HOME=$env:JAVA_HOME"
if($env:JDK_HOME)
{
$env:JDK_HOME = $env:JAVA_HOME
Write-Verbose "JDK_HOME=$env:JDK_HOME"
}
if($env:JRE_HOME -and (Test-Path "$env:JAVA_HOME\jre" -PathType Container))
{
$env:JRE_HOME = "$env:JAVA_HOME\jre"
Write-Verbose "JRE_HOME=$env:JRE_HOME"
}
if($env:CLASSPATH)
{
$env:CLASSPATH = ".;$env:JAVA_HOME\lib;$env:JAVA_HOME\jre\lib"
Write-Verbose "CLASSPATH=$env:CLASSPATH"
}
$env:Path = ($env:Path -split ';' |% {if($_ -match 'Java|JDK'){"$Path\bin"}else{$_}}) -join ';'
Write-Verbose "Path=$($env:Path -replace ';',([Environment]::NewLine))"
${java.exe} = (Get-Command java.exe -CommandType Application).Path
Write-Verbose "Using Java ${java.exe} ($((Get-Item ${java.exe}).VersionInfo.ProductVersion))"