Skip to content

Commit e453530

Browse files
authored
Merge pull request jruby#38 from headius/macos_java_home
Use java_home command to find it when not set
2 parents 8878681 + 4d0a15b commit e453530

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

unixlauncher.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <stdlib.h>
22
#include <unistd.h>
3+
#include <limits.h>
4+
#include <string.h>
35
#include "unixlauncher.h"
46
#include "utilsfuncs.h"
57

@@ -67,11 +69,27 @@ int UnixLauncher::run(int argc, char* argv[], char* envp[]) {
6769
return 255;
6870
}
6971

70-
// still no jdk home, use java command to resolve it
72+
// still no jdk home, use other means to resolve it
7173
if (jdkhome.empty()) {
72-
java = resolveSymlinks(java);
73-
int home_index = java.find_last_of('/', java.find_last_of('/') - 1);
74-
jdkhome = java.substr(0, home_index);
74+
if (access("/usr/libexec/java_home", X_OK) != -1) {
75+
// try java_home command when not set (on MacOS)
76+
FILE *fp;
77+
char tmp[PATH_MAX + 1];
78+
79+
fp = popen("/usr/libexec/java_home", "r");
80+
if (fp != NULL) {
81+
fgets(tmp, sizeof(tmp), fp);
82+
tmp[strcspn(tmp, "\n")] = 0;
83+
jdkhome = tmp;
84+
pclose(fp);
85+
} else {
86+
logErr(true, false, "failed to run /usr/libexec/java_home");
87+
}
88+
} else {
89+
java = resolveSymlinks(java);
90+
int home_index = java.find_last_of('/', java.find_last_of('/') - 1);
91+
jdkhome = java.substr(0, home_index);
92+
}
7593
}
7694

7795
prepareOptions();

0 commit comments

Comments
 (0)