mccl/pkg/util/locatejavahome_windows.go

72 lines
1.9 KiB
Go

//go:build windows
// +build windows
package util
import (
"fmt"
"strconv"
"strings"
"golang.org/x/sys/windows/registry"
)
const legacyJavaRegPath = "SOFTWARE\\JavaSoft\\Java Runtime Environment"
const jdkRegPath = "SOFTWARE\\JavaSoft\\JDK"
func LocateJavaHome(component string, majorVersion int) (string, error) {
if component == "jre-legacy" {
jre, err := registry.OpenKey(registry.LOCAL_MACHINE, legacyJavaRegPath, registry.QUERY_VALUE)
if err != nil {
return "", err
}
curVer, _, err := jre.GetStringValue("CurrentVersion")
if err != nil {
return "", nil
}
mVer, err := strconv.ParseInt(strings.Split(curVer, ".")[1], 10, 64)
if err != nil {
return "", nil
}
if mVer < int64(majorVersion) {
return "", fmt.Errorf("installed JRE version %s is older than allowed 1.%d", curVer, majorVersion)
}
jre, err = registry.OpenKey(registry.LOCAL_MACHINE, fmt.Sprintf("%s\\%s", legacyJavaRegPath, curVer), registry.QUERY_VALUE)
if err != nil {
return "", err
}
javaHome, _, err := jre.GetStringValue("JavaHome")
if err != nil {
return "", err
}
return javaHome, nil
} else {
jdk, err := registry.OpenKey(registry.LOCAL_MACHINE, jdkRegPath, registry.QUERY_VALUE)
if err != nil {
return "", err
}
curVer, _, err := jdk.GetStringValue("CurrentVersion")
if err != nil {
return "", nil
}
mVer, err := strconv.ParseInt(strings.Split(curVer, ".")[0], 10, 64)
if err != nil {
return "", nil
}
if mVer < int64(majorVersion) {
return "", fmt.Errorf("installed JDK version %d is older than allowed %d", mVer, majorVersion)
}
jdk, err = registry.OpenKey(registry.LOCAL_MACHINE, fmt.Sprintf("%s\\%s", jdkRegPath, curVer), registry.QUERY_VALUE)
if err != nil {
return "", err
}
javaHome, _, err := jdk.GetStringValue("JavaHome")
if err != nil {
return "", err
}
return javaHome, nil
}
return "", nil
}