Skip to content

Commit 4aa28b2

Browse files
committed
Fix JRUBY-6620: On windows if JAVA_HOME ends in a '\' then native launcher bombs
Before processing JAVA_HOME, trim the trailing backslashes, which could cause problems.
1 parent 7d0e47e commit 4aa28b2

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

platformlauncher.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ bool PlatformLauncher::checkJDKHome() {
220220
logMsg("-Xjdkhome is not set, checking for %%JAVA_HOME%%...");
221221
char *javaHome = getenv("JAVA_HOME");
222222
if (javaHome) {
223+
javaHome = trimTrailingBackslashes(javaHome);
223224
logMsg("%%JAVA_HOME%% is set: %s", javaHome);
224225
if (!jvmLauncher.initialize(javaHome)) {
225226
logMsg("ERROR: Cannot locate java installation, specified by JAVA_HOME: %s", javaHome);

spec/launcher_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
end
5050
end
5151

52+
it "should drop the backslashes at the end of JAVA_HOME" do
53+
with_environment "JAVA_HOME" => File.join("some", "java", "home\\\\") do
54+
if windows?
55+
jruby_launcher_args("").join.should =~ %r{some/java/home}
56+
else
57+
jruby_launcher_args("").first.should == File.join("some", "java", "home", "bin", "java")
58+
end
59+
end
60+
end
61+
5262
it "should complain about a missing log argument" do
5363
jruby_launcher("-Xtrace 2>&1").should =~ /Argument is missing for "-Xtrace"/
5464
jruby_launcher("-Xtrace -- 2>&1").should =~ /Argument is missing for "-Xtrace"/

unixlauncher.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ int UnixLauncher::run(int argc, char* argv[], char* envp[]) {
5252
if (!jdkhome.empty()) {
5353
java = jdkhome + "/bin/java";
5454
} else if (getenv("JAVA_HOME") != NULL) {
55-
java = string(getenv("JAVA_HOME")) + "/bin/java";
55+
string java_home = string(getenv("JAVA_HOME"));
56+
java_home = trimTrailingBackslashes(java_home);
57+
java = java_home + "/bin/java";
5658
} else {
5759
java = findOnPath("java");
5860
}

utilsfuncs.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,11 @@ void printListToConsole(list<string> values) {
269269
std::cout << *it << std::endl;
270270
}
271271
}
272+
273+
string trimTrailingBackslashes(string orig) {
274+
while (orig.size() > 0 &&
275+
orig.at(orig.size() - 1) == '\\') {
276+
orig.erase(orig.size() - 1);
277+
}
278+
return orig;
279+
}

utilsfuncs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ void addToArgList(std::list<std::string> & args, int argc, char ** argv);
5959
std::string findOnPath(const char* name);
6060
bool checkDirectory(const char* path);
6161
void printListToConsole(std::list<std::string> l);
62+
std::string trimTrailingBackslashes(std::string orig);
6263

6364
#ifndef WIN32
6465
#define FILE_SEP '/'

0 commit comments

Comments
 (0)