Skip to content

Commit bd5f648

Browse files
committed
Clarify missing java error message (#46160)
Since the bundled jdk was added to Elasticsearch, there are now 2 ways java can be missing. Either JAVA_HOME is set but does not exist, or the bundled jdk does not exist. This commit improves the error messages in those two cases, and also ensures our tests cover both cases.
1 parent 5cfcd7c commit bd5f648

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

distribution/src/bin/elasticsearch-env

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,21 @@ ES_CLASSPATH="$ES_HOME/lib/*"
3838
# now set the path to java
3939
if [ ! -z "$JAVA_HOME" ]; then
4040
JAVA="$JAVA_HOME/bin/java"
41+
JAVA_TYPE="JAVA_HOME"
4142
else
4243
if [ "$(uname -s)" = "Darwin" ]; then
4344
# OSX has a different structure
4445
JAVA="$ES_HOME/jdk/Contents/Home/bin/java"
4546
else
4647
JAVA="$ES_HOME/jdk/bin/java"
4748
fi
49+
JAVA_TYPE="bundled jdk"
4850
fi
4951

5052
if [ ! -x "$JAVA" ]; then
51-
echo "could not find java in JAVA_HOME or bundled at $JAVA" >&2
52-
exit 1
53-
fi
53+
echo "could not find java in $JAVA_TYPE at $JAVA" >&2
54+
exit 1
55+
fi
5456

5557
# do not let JAVA_TOOL_OPTIONS slip in (as the JVM does by default)
5658
if [ ! -z "$JAVA_TOOL_OPTIONS" ]; then

distribution/src/bin/elasticsearch-env.bat

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ if "%1" == "nojava" (
3838

3939
if defined JAVA_HOME (
4040
set JAVA="%JAVA_HOME%\bin\java.exe"
41+
set JAVA_TYPE=JAVA_HOME
4142
) else (
4243
set JAVA="%ES_HOME%\jdk\bin\java.exe"
4344
set JAVA_HOME="%ES_HOME%\jdk"
45+
set JAVA_TYPE=bundled jdk
4446
)
4547

46-
if not exist %JAVA% (
47-
echo "could not find java in JAVA_HOME or bundled at %ES_HOME%\jdk" >&2
48+
if not exist !JAVA! (
49+
echo "could not find java in !JAVA_TYPE! at !JAVA!" >&2
4850
exit /b 1
4951
)
5052

qa/os/src/test/java/org/elasticsearch/packaging/test/ArchiveTests.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void test20PluginsListWithNoPlugins() throws Exception {
7474
assertThat(r.stdout, isEmptyString());
7575
}
7676

77-
public void test30NoJava() throws Exception {
77+
public void test30MissingBundledJdk() throws Exception {
7878
final Installation.Executables bin = installation.executables();
7979
sh.getEnv().remove("JAVA_HOME");
8080

@@ -87,14 +87,25 @@ public void test30NoJava() throws Exception {
8787
// ask for elasticsearch version to quickly exit if java is actually found (ie test failure)
8888
final Result runResult = sh.runIgnoreExitCode(bin.elasticsearch.toString() + " -v");
8989
assertThat(runResult.exitCode, is(1));
90-
assertThat(runResult.stderr, containsString("could not find java in JAVA_HOME or bundled"));
90+
assertThat(runResult.stderr, containsString("could not find java in bundled jdk"));
9191
} finally {
9292
if (distribution().hasJdk) {
9393
mv(relocatedJdk, installation.bundledJdk);
9494
}
9595
}
9696
}
9797

98+
public void test31BadJavaHome() throws Exception {
99+
final Installation.Executables bin = installation.executables();
100+
sh.getEnv().put("JAVA_HOME", "doesnotexist");
101+
102+
// ask for elasticsearch version to quickly exit if java is actually found (ie test failure)
103+
final Result runResult = sh.runIgnoreExitCode(bin.elasticsearch.toString() + " -v");
104+
assertThat(runResult.exitCode, is(1));
105+
assertThat(runResult.stderr, containsString("could not find java in JAVA_HOME"));
106+
107+
}
108+
98109
public void test40CreateKeystoreManually() throws Exception {
99110
final Installation.Executables bin = installation.executables();
100111

@@ -178,7 +189,7 @@ public void test52BundledJdkRemoved() throws Exception {
178189

179190
public void test53JavaHomeWithSpecialCharacters() throws Exception {
180191
Platforms.onWindows(() -> {
181-
final Shell sh = new Shell();
192+
final Shell sh = newShell();
182193
try {
183194
// once windows 2012 is no longer supported and powershell 5.0 is always available we can change this command
184195
sh.run("cmd /c mklink /D 'C:\\Program Files (x86)\\java' $Env:SYSTEM_JAVA_HOME");
@@ -201,7 +212,7 @@ public void test53JavaHomeWithSpecialCharacters() throws Exception {
201212
});
202213

203214
Platforms.onLinux(() -> {
204-
final Shell sh = new Shell();
215+
final Shell sh = newShell();
205216
// Create temporary directory with a space and link to real java home
206217
String testJavaHome = Paths.get("/tmp", "java home").toString();
207218
try {

qa/os/src/test/java/org/elasticsearch/packaging/test/WindowsServiceTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,27 @@ public void test12InstallService() {
117117
sh.run(serviceScript + " remove");
118118
}
119119

120-
public void test13InstallMissingJava() throws IOException {
120+
public void test13InstallMissingBundledJdk() throws IOException {
121121
final Path relocatedJdk = installation.bundledJdk.getParent().resolve("jdk.relocated");
122122

123123
try {
124124
mv(installation.bundledJdk, relocatedJdk);
125125
Result result = sh.runIgnoreExitCode(serviceScript + " install");
126126
assertThat(result.exitCode, equalTo(1));
127-
assertThat(result.stderr, containsString("could not find java in JAVA_HOME or bundled"));
127+
assertThat(result.stderr, containsString("could not find java in bundled jdk"));
128128
} finally {
129129
mv(relocatedJdk, installation.bundledJdk);
130130
}
131131
}
132132

133-
public void test14RemoveNotInstalled() {
133+
public void test14InstallBadJavaHome() throws IOException {
134+
sh.getEnv().put("JAVA_HOME", "doesnotexist");
135+
Result result = sh.runIgnoreExitCode(serviceScript + " install");
136+
assertThat(result.exitCode, equalTo(1));
137+
assertThat(result.stderr, containsString("could not find java in JAVA_HOME"));
138+
}
139+
140+
public void test15RemoveNotInstalled() {
134141
Result result = assertFailure(serviceScript + " remove", 1);
135142
assertThat(result.stdout, containsString("Failed removing '" + DEFAULT_ID + "' service"));
136143
}

qa/os/src/test/java/org/elasticsearch/packaging/util/ServerUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static void waitForElasticsearch(String status, String index) throws IOEx
7272

7373
} catch (HttpHostConnectException e) {
7474
// we want to retry if the connection is refused
75-
LOG.debug("Got connection refused when waiting for cluster health", e);
75+
LOG.info("Got connection refused when waiting for cluster health", e);
7676
}
7777

7878
timeElapsed = System.currentTimeMillis() - startTime;

0 commit comments

Comments
 (0)