From 16b6098df8dbec4f044e1a450fb40ca5789d8f41 Mon Sep 17 00:00:00 2001 From: Charlie Voiselle <464492+angrycub@users.noreply.github.com> Date: Sun, 1 Nov 2020 13:20:31 -0500 Subject: [PATCH] Fix for Java fingerprinter on macOS (#9225) Use alternative test for macOS JVM with /usr/libexec/java_home --- drivers/java/driver.go | 12 ++++++++++++ drivers/java/utils.go | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/drivers/java/driver.go b/drivers/java/driver.go index 220935046..7696e115e 100644 --- a/drivers/java/driver.go +++ b/drivers/java/driver.go @@ -223,6 +223,18 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint { return fp } } + if runtime.GOOS == "darwin" { + _, err := checkForMacJVM() + if err != nil { + // return no error, as it isn't an error to not find java, it just means we + // can't use it. + + fp.Health = drivers.HealthStateUndetected + fp.HealthDescription = "" + d.logger.Trace("macOS jvm not found", "error", err) + return fp + } + } version, runtime, vm, err := javaVersionInfo() if err != nil { diff --git a/drivers/java/utils.go b/drivers/java/utils.go index b41eec691..1615ea643 100644 --- a/drivers/java/utils.go +++ b/drivers/java/utils.go @@ -5,14 +5,38 @@ import ( "fmt" "os/exec" "regexp" + rt "runtime" "strings" ) var javaVersionCommand = []string{"java", "-version"} +var macOSJavaTestCommand = "/usr/libexec/java_home" + +func checkForMacJVM() (ok bool, err error) { + // test for java differently because of the shim application + var out bytes.Buffer + cmd := exec.Command(macOSJavaTestCommand) + cmd.Stdout = &out + cmd.Stderr = &out + err = cmd.Run() + if err != nil { + err = fmt.Errorf("failed check for macOS jvm: %v, out: %v", err, strings.Replace(strings.Replace(out.String(), "\n", " ", -1), `"`, `\"`, -1)) + return false, err + } + return true, nil +} func javaVersionInfo() (version, runtime, vm string, err error) { var out bytes.Buffer + if rt.GOOS == "darwin" { + _, err = checkForMacJVM() + if err != nil { + err = fmt.Errorf("failed to check java version: %v", err) + return + } + } + cmd := exec.Command(javaVersionCommand[0], javaVersionCommand[1:]...) cmd.Stdout = &out cmd.Stderr = &out