Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle spaces in java library path #106789

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public static String getTestLibraryPath(String nativeLibsDir) {
String platform = String.format(Locale.ROOT, "%s-%s", ElasticsearchDistribution.CURRENT_PLATFORM, arch);
String existingLibraryPath = System.getProperty("java.library.path");

return String.format(Locale.ROOT, "%s/%s%c%s", nativeLibsDir, platform, File.pathSeparatorChar, existingLibraryPath);
String format = "%s/%s%c%s";
if (ElasticsearchDistribution.CURRENT_PLATFORM.equals(ElasticsearchDistribution.Platform.WINDOWS)) {
// windows doesn't like spaces in paths, so we must wrap the entire path in quotes to guard for it
format = "\"" + format + "\"";
}

return String.format(Locale.ROOT, format, nativeLibsDir, platform, File.pathSeparatorChar, existingLibraryPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -157,10 +158,13 @@ private static String findLibraryPath(Map<String, String> sysprops) {
String existingPath = sysprops.get("java.library.path");
assert existingPath != null;

String format = "%s%c%s";
String osname = sysprops.get("os.name");
String os;
if (osname.startsWith("Windows")) {
os = "windows";
// windows doesn't like spaces in paths, so we must wrap the entire path in quotes to guard for it
format = "\"" + format + "\"";
} else if (osname.startsWith("Linux")) {
os = "linux";
} else if (osname.startsWith("Mac OS")) {
Expand All @@ -177,11 +181,13 @@ private static String findLibraryPath(Map<String, String> sysprops) {
} else {
arch = "unsupported_arch[" + archname + "]";
}
return platformDir.resolve(os + "-" + arch).toAbsolutePath() + getPathSeparator() + existingPath;

String esPlatformDir = platformDir.resolve(os + "-" + arch).toAbsolutePath().toString();
return String.format(Locale.ROOT, format, esPlatformDir, getPathSeparator(), existingPath);
}

@SuppressForbidden(reason = "no way to get path separator with nio")
private static String getPathSeparator() {
return File.pathSeparator;
private static char getPathSeparator() {
return File.pathSeparatorChar;
}
}