Browse Source

Use bundled JDK in Sys V init (#45593)

This commit addresses an issue when trying to using Elasticsearch on
systems with Sys V init and the bundled JDK was not being used. Instead,
we were still inadvertently trying to fallback on the path. This commit
removes that fallback as that is against our intentions for 7.x where we
only support the bundled JDK or an explicit JDK via JAVA_HOME.
Jason Tedor 6 years ago
parent
commit
56cb10e7d2

+ 10 - 10
distribution/packages/src/deb/init.d/elasticsearch

@@ -81,16 +81,16 @@ if [ ! -x "$DAEMON" ]; then
 fi
 
 checkJava() {
-	if [ -x "$JAVA_HOME/bin/java" ]; then
-		JAVA="$JAVA_HOME/bin/java"
-	else
-		JAVA=`which java`
-	fi
-
-	if [ ! -x "$JAVA" ]; then
-		echo "Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME"
-		exit 1
-	fi
+  if [ ! -z "${JAVA_HOME}" ]; then
+    JAVA="${JAVA_HOME}"/bin/java
+  else
+    JAVA="${ES_HOME}"/jdk/bin/java
+  fi
+
+  if [ ! -x "$JAVA" ]; then
+    echo "could not find java in JAVA_HOME or bundled at ${JAVA}"
+    exit 1
+  fi
 }
 
 case "$1" in

+ 10 - 10
distribution/packages/src/rpm/init.d/elasticsearch

@@ -68,16 +68,16 @@ if [ ! -x "$exec" ]; then
 fi
 
 checkJava() {
-    if [ -x "$JAVA_HOME/bin/java" ]; then
-        JAVA="$JAVA_HOME/bin/java"
-    else
-        JAVA=`which java`
-    fi
-
-    if [ ! -x "$JAVA" ]; then
-        echo "Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME"
-        exit 1
-    fi
+  if [ ! -z "${JAVA_HOME}" ]; then
+    JAVA="${JAVA_HOME}"/bin/java
+  else
+    JAVA="${ES_HOME}"/jdk/bin/java
+  fi
+
+  if [ ! -x "$JAVA" ]; then
+    echo "could not find java in JAVA_HOME or bundled at ${JAVA}"
+    exit 1
+  fi
 }
 
 start() {

+ 19 - 0
qa/os/src/test/java/org/elasticsearch/packaging/test/PackageTestCase.java

@@ -136,6 +136,25 @@ public abstract class PackageTestCase extends PackagingTestCase {
         assertRunsWithJavaHome();
     }
 
+    public void test33RunsIfJavaNotOnPath() throws Exception {
+        assumeThat(distribution().hasJdk, is(true));
+
+        final Result readlink = sh.run("readlink /usr/bin/java");
+        boolean unlinked = false;
+        try {
+            sh.run("unlink /usr/bin/java");
+            unlinked = true;
+
+            startElasticsearch(sh);
+            runElasticsearchTests();
+            stopElasticsearch(sh);
+        } finally {
+            if (unlinked) {
+                sh.run("ln -sf " + readlink.stdout.trim() + " /usr/bin/java");
+            }
+        }
+    }
+
     public void test42BundledJdkRemoved() throws Exception {
         assumeThat(installation, is(notNullValue()));
         assumeThat(distribution().hasJdk, is(true));