forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Use-Java.ps1
44 lines (40 loc) · 1.44 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
40
41
42
43
44
<#
.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.
.FUNCTIONALITY
System and updates
.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
)
Process
{
$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 ';' |ForEach-Object {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))"
}