Skip to content

Instantly share code, notes, and snippets.

@LXGaming
Last active June 3, 2023 07:10
Show Gist options
  • Save LXGaming/159d8b62c0c24625744d544cf0036917 to your computer and use it in GitHub Desktop.
Save LXGaming/159d8b62c0c24625744d544cf0036917 to your computer and use it in GitHub Desktop.
Java Runtime PowerShell Script
$DefaultPackage = ""
$DefaultVersion = ""
class Runtime {
[System.IO.FileInfo] $Executable
[System.IO.DirectoryInfo] $Directory
[string] $Package
[Version] $Version
Runtime([System.IO.FileInfo] $Executable) {
$this.Executable = $Executable
$this.Directory = $Executable.Directory.Parent
if ($this.Directory.Name -eq "jre") {
$this.Package = "jre"
$this.Version = [Version]::new($this.Directory.Parent.Name)
} elseif ($this.Directory.Name.EndsWith("jre")) {
$this.Package = "jre"
$this.Version = [Version]::new($this.Directory.Name)
} else {
$this.Package = "jdk"
$this.Version = [Version]::new($this.Directory.Name)
}
}
[string] ToString() {
return ("{0}-{1}", $this.Package, $this.Version)
}
}
class Version {
[int] $Major
[int] $Minor
[int] $Patch
Version([int] $Major, [int] $Minor, [int] $Patch) {
$this.Major = $Major
$this.Minor = $Minor
$this.Patch = $Patch
}
Version([string] $string) {
$Match = [regex]::Match($string, "(\d+)\.(\d+)\.(\d+)")
if ($Match.Success) {
$this.Major = $Match.Groups[1].Value
$this.Minor = $Match.Groups[2].Value
$this.Patch = $Match.Groups[3].Value
return
}
$Match = [regex]::Match($string, "(\d)u(\d+)")
if ($Match.Success) {
$this.Major = $Match.Groups[1].Value
$this.Minor = 0
$this.Patch = $Match.Groups[2].Value
return
}
}
[boolean] Match([string] $string) {
if ($this.Major -eq $string) {
return $true
}
if ($this.ToString().StartsWith($string)) {
return $true
}
return $false
}
[string] ToString() {
if ($this.Major -eq 0 -and $this.Minor -eq 0 -and $this.Patch -eq 0) {
return "Unknown"
}
if ($this.Major -le 8) {
return ("1.{0}.0_{1}" -f $this.Major, $this.Patch)
}
return ("{0}.{1}.{2}" -f $this.Major, $this.Minor, $this.Patch)
}
}
function Get-Executable([string] $Platform) {
if ($Platform -eq "windows") {
return "java.exe"
} else {
return "java"
}
}
function Get-Paths([string] $Platform) {
if ($Platform -eq "windows") {
return @(
"C:\Program Files\AdoptOpenJDK",
"C:\Program Files\Eclipse Adoptium",
"C:\Program Files\Java",
"$env:JAVA_HOME",
"$env:USERPROFILE\.gradle\jdks"
)
} else {
return @()
}
}
function Get-Platform() {
if (!$PSVersionTable -or !$PSVersionTable.PSEdition) {
throw "PowerShell Edition Unavailable"
} elseif ($PSVersionTable.PSEdition -eq "Core") {
if (!$PSVersionTable.Platform) {
throw "PowerShell Platform Unavailable"
} elseif ($PSVersionTable.Platform -eq "Unix") {
return "linux"
} elseif ($PSVersionTable.Platform -eq "Win32NT") {
return "windows"
} else {
throw "PowerShell Platform Unsupported"
}
} elseif ($PSVersionTable.PSEdition -eq "Desktop") {
return "windows"
} else {
throw "PowerShell Edition Unsupported"
}
}
function Get-Registries([string] $Platform) {
if ($Platform -ne "windows") {
return @{}
}
return @{
"Adoptium" = @(
"HKLM:\SOFTWARE\Eclipse Adoptium\JDK",
"HKLM:\SOFTWARE\Eclipse Adoptium\JRE"
)
"Oracle" = @(
"HKLM:\SOFTWARE\JavaSoft\Java Development Kit",
"HKLM:\SOFTWARE\JavaSoft\Java Runtime Environment",
"HKLM:\SOFTWARE\JavaSoft\JDK",
"HKLM:\SOFTWARE\JavaSoft\JRE"
)
}
}
function Get-Runtimes([string] $Platform, [string] $Package, [string] $Version) {
[string] $Executable = Get-Executable -Platform $Platform
[array] $Paths = Get-Paths -Platform $Platform
if (!$Paths -or $Paths.Count -eq 0) {
throw "Paths Unavailable"
}
$Registries = Get-Registries -Platform $Platform
ForEach ($PackagePath in $Registries["Adoptium"]) {
if (!$PackagePath -or !(Test-Path -LiteralPath $PackagePath)) {
continue
}
$VersionPaths = Get-ChildItem -LiteralPath $PackagePath -Name | ForEach-Object { Join-Path -Path $PackagePath -ChildPath $_ }
if (!$VersionPaths) {
continue
}
ForEach ($VersionPath in $VersionPaths) {
$JVMPaths = Get-ChildItem -LiteralPath $VersionPath -Name | ForEach-Object { Join-Path -Path $VersionPath -ChildPath $_ }
if (!$JVMPaths) {
continue
}
ForEach ($JVMPath in $JVMPaths) {
$Paths += Get-ItemPropertyValue -LiteralPath (Join-Path -Path $JVMPath -ChildPath "MSI") -Name "Path"
}
}
}
ForEach ($PackagePath in $Registries["Oracle"]) {
if (!$PackagePath -or !(Test-Path -Path $PackagePath)) {
continue
}
$VersionPaths = Get-ChildItem -LiteralPath $PackagePath -Name | ForEach-Object { Join-Path -Path $PackagePath -ChildPath $_ }
if (!$VersionPaths) {
continue
}
ForEach ($VersionPath in $VersionPaths) {
$Paths += Get-ItemPropertyValue -LiteralPath $VersionPath -Name "JavaHome"
}
}
$Entries = @()
ForEach ($Path in $Paths | Select-Object -Unique) {
if (!$Path -or !(Test-Path -LiteralPath $Path)) {
continue
}
ForEach ($File in Get-ChildItem -LiteralPath $Path -Filter $Executable -File -Depth 2) {
if ($Entries | Where-Object { $_.Executable -eq $File.FullName }) {
continue
}
$Runtime = [Runtime]::new($File)
if ($Package -and $Runtime.Package -ne $Package) {
continue
}
if ($Version -and !$Runtime.Version.Match($Version)) {
continue
}
$Entries += [Runtime]::new($File)
}
}
if ($Entries.Count -eq 0) {
if ($Package -or $Version) {
throw "Configured Java Runtime Unavailable"
}
throw "Java Runtime Unavailable"
}
return $Entries | Sort-Object { $_.Executable.FullName } -Unique | Sort-Object -Property Version
}
$Platform = Get-Platform
$Package = $DefaultPackage
$Version = $DefaultVersion
$Key = $args[0]
if ($Key -and $Key.StartsWith("-")) {
$Key = $Key.TrimStart("-")
if ($Key -eq "List-Runtimes" -or $Key -eq "ListRuntimes") {
[array] $Entries = Get-Runtimes -Platform $Platform
$Padding = ($Entries | Measure-Object -InputObject { $_.Version.ToString().Length } -Maximum).Maximum
ForEach ($Entry in $Entries) {
Write-Host "$($Entry.Version.ToString().PadRight($Padding, " ")) [$($Entry.Directory.FullName)]"
}
return
}
if ($Key -eq "Runtime") {
$Value = $args[1]
if (!$Value) {
throw "Expected a value after Runtime"
}
$Version = $Value
$args = $args[2..($args.Length)]
}
}
[array] $Entries = Get-Runtimes -Platform $Platform -Package $Package -Version $Version
& $Entries[-1].Executable.FullName $args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment