Skip to content

Commit

Permalink
Bat scripts to work with JAVA_HOME with parantheses
Browse files Browse the repository at this point in the history
the elasticsearch.bat and elasticsearch-env.bat won't work if JAVA
contains parentheses. This seems to be the limitation of FOR /F IN
(command) DO syntax.
The JAVA variable present in a command contains a path to a binary to
start elasticsearch (with spaces & parans). We can workaround the
problem of spaces and parentheses in this path by referring this
variable with a CALL command.
Note that executing binaries with CALL is an undocumented behaviour (but
works)
  • Loading branch information
pgomulka committed Mar 6, 2019
1 parent 5e74e82 commit fe6435b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch-env.bat
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ set ES_DISTRIBUTION_FLAVOR=${es.distribution.flavor}
set ES_DISTRIBUTION_TYPE=${es.distribution.type}

if not defined ES_TMPDIR (
for /f "tokens=* usebackq" %%a in (`"%JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.TempDirectory""`) do set ES_TMPDIR=%%a
for /f "tokens=* usebackq" %%a in (`CALL %JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.TempDirectory"`) do set ES_TMPDIR=%%a
)
5 changes: 3 additions & 2 deletions distribution/src/bin/elasticsearch.bat
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ IF ERRORLEVEL 1 (
EXIT /B %ERRORLEVEL%
)

set "ES_JVM_OPTIONS=%ES_PATH_CONF%\jvm.options"
set ES_JVM_OPTIONS=%ES_PATH_CONF%\jvm.options
@setlocal
for /F "usebackq delims=" %%a in (`"%JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.JvmOptionsParser" "!ES_JVM_OPTIONS!" || echo jvm_options_parser_failed"`) do set JVM_OPTIONS=%%a
for /F "usebackq delims=" %%a in (`CALL %JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.JvmOptionsParser" "!ES_JVM_OPTIONS!"
^|^| echo jvm_options_parser_failed`) do set JVM_OPTIONS=%%a
@endlocal & set "MAYBE_JVM_OPTIONS_PARSER_FAILED=%JVM_OPTIONS%" & set ES_JAVA_OPTS=%JVM_OPTIONS:${ES_TMPDIR}=!ES_TMPDIR!% %ES_JAVA_OPTS%

if "%MAYBE_JVM_OPTIONS_PARSER_FAILED%" == "jvm_options_parser_failed" (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;
import static org.elasticsearch.packaging.util.Archives.ARCHIVE_OWNER;
import static org.elasticsearch.packaging.util.Archives.installArchive;
import static org.elasticsearch.packaging.util.Archives.verifyArchiveInstallation;
Expand Down Expand Up @@ -182,6 +184,39 @@ public void test52BundledJdkRemoved() throws IOException {
}
}

public void test53JavaHomeContainParansAndSpace() throws IOException {
assumeThat(installation, is(notNullValue()));

Platforms.onWindows(() -> {
final Shell sh = new Shell();
final String originalPath = sh.run("$Env:PATH").stdout.trim();
final String javaHome = sh.run("$Env:JAVA_HOME").stdout.trim();

try {
final String newPath = Arrays.stream(originalPath.split(";"))
.filter(path -> path.contains("Java") == false)
.collect(joining(";"));

sh.runIgnoreExitCode("cmd /c mklink /D 'C:\\Program Files (x86)\\java' $Env:JAVA_HOME");

sh.getEnv().put("PATH", newPath);
sh.getEnv().put("JAVA_HOME", "C:\\Program Files (x86)\\java");

Archives.runElasticsearch(installation, sh);

Archives.stopElasticsearch(installation);
} catch (IOException e) {
logger.error("Test failed with exception", e);
}finally {
//clean up sym link
sh.runIgnoreExitCode("cmd /c del /F /Q 'C:\\Program Files (x86)\\java' ");
sh.getEnv().put("PATH", originalPath);
sh.getEnv().put("JAVA_HOME", javaHome);

}
});
}

public void test60AutoCreateKeystore() {
assumeThat(installation, is(notNullValue()));

Expand Down

0 comments on commit fe6435b

Please sign in to comment.