2018-10-31 14:30:13 +00:00
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
2020-11-01 18:20:31 +00:00
|
|
|
rt "runtime"
|
2018-10-31 14:30:13 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var javaVersionCommand = []string{"java", "-version"}
|
2020-11-01 18:20:31 +00:00
|
|
|
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 {
|
2021-04-03 07:50:23 +00:00
|
|
|
err = fmt.Errorf("failed check for macOS jvm: %v, out: %v", err, strings.ReplaceAll(strings.ReplaceAll(out.String(), "\n", " "), `"`, `\"`))
|
2020-11-01 18:20:31 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2018-10-31 14:30:13 +00:00
|
|
|
|
|
|
|
func javaVersionInfo() (version, runtime, vm string, err error) {
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
2020-11-01 18:20:31 +00:00
|
|
|
if rt.GOOS == "darwin" {
|
|
|
|
_, err = checkForMacJVM()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("failed to check java version: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 14:30:13 +00:00
|
|
|
cmd := exec.Command(javaVersionCommand[0], javaVersionCommand[1:]...)
|
|
|
|
cmd.Stdout = &out
|
|
|
|
cmd.Stderr = &out
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
2019-02-12 18:41:26 +00:00
|
|
|
err = fmt.Errorf("failed to check java version: %v", err)
|
2018-10-31 14:30:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-12 18:41:26 +00:00
|
|
|
version, runtime, vm = parseJavaVersionOutput(out.String())
|
2018-10-31 14:30:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-15 19:14:58 +00:00
|
|
|
var (
|
|
|
|
javaVersionRe = regexp.MustCompile(`([.\d_]+)`)
|
|
|
|
)
|
|
|
|
|
2019-02-12 18:41:26 +00:00
|
|
|
func parseJavaVersionOutput(infoString string) (version, runtime, vm string) {
|
2018-10-31 14:30:13 +00:00
|
|
|
infoString = strings.TrimSpace(infoString)
|
|
|
|
|
|
|
|
lines := strings.Split(infoString, "\n")
|
2019-01-08 21:17:47 +00:00
|
|
|
if strings.Contains(lines[0], "Picked up _JAVA_OPTIONS") {
|
|
|
|
lines = lines[1:]
|
|
|
|
}
|
|
|
|
|
2019-02-12 18:41:26 +00:00
|
|
|
if len(lines) < 3 {
|
|
|
|
// unexpected output format, don't attempt to parse output for version
|
|
|
|
return "", "", ""
|
2018-10-31 14:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
versionString := strings.TrimSpace(lines[0])
|
|
|
|
|
2021-06-15 19:14:58 +00:00
|
|
|
if match := javaVersionRe.FindStringSubmatch(versionString); len(match) == 2 {
|
2018-10-31 14:30:13 +00:00
|
|
|
versionString = match[1]
|
|
|
|
}
|
|
|
|
|
2019-02-12 18:41:26 +00:00
|
|
|
return versionString, strings.TrimSpace(lines[1]), strings.TrimSpace(lines[2])
|
2018-10-31 14:30:13 +00:00
|
|
|
}
|