Bug 61441

Summary: daemon.sh's auto-detection fails on linux system's where java is installed via an RPM
Product: Tomcat 8 Reporter: Coty Sutherland <csutherl>
Component: PackagingAssignee: Tomcat Developers Mailing List <dev>
Severity: normal    
Priority: P2    
Version: 8.5.x-trunk   
Target Milestone: ----   
Hardware: PC   
OS: Linux   
Attachments: Option to add fallbacks
Another seemingly viable option

Description Coty Sutherland 2017-08-17 18:44:37 UTC
On RHEL/Centos/Fedora, when java is installed from the RPMs neither JAVA_HOME nor JRE_HOME environment variables are set. Luckily, the Tomcat startup scripts are able to auto-detect them but the daemon.sh script cannot properly do so. The failure is in the JAVA_HOME auto-detection in the script here:


JAVA_BIN="`which java 2>/dev/null || type java 2>&1`"
test -x "$JAVA_BIN" && JAVA_HOME="`dirname $JAVA_BIN`"
test ".$JAVA_HOME" != . && JAVA_HOME=`cd "$JAVA_HOME/.." >/dev/null; pwd`

The problem is that when java is installed from the RPMs, `which java` evaluates to '/usr/bin/java', which is a symlink maintained by alternatives. The code snippet above will evaluate to JAVA_HOME and JRE_HOME being set to '/usr'. Having JRE_HOME set to '/usr' doesn't seem to cause problems for Tomcat, but the problem manifests itself when running the daemon.sh script:

# ./daemon.sh start
Cannot find any VM in Java Home /usr
jsvc is only used in daemon.sh, therefore this problem was not seen when starting Tomcat using startup.sh or catalina.sh.

I think a good fallback for JAVA_HOME and JRE_HOME is '/usr/lib/jvm/java' and '/usr/lib/jvm/jre' respectively.

To Reproduce:

1. Install tomcat
2. Install java from RPMs
4. bin/daemon.sh start
5. Observe error mentioned above
Comment 1 Coty Sutherland 2017-08-17 18:45:51 UTC
Created attachment 35246 [details]
Option to add fallbacks

This patch would just add a check for JAVA_HOME being /usr and if it is, use the correct directories for the JDK or JRE installation.
Comment 2 Coty Sutherland 2017-08-17 18:46:54 UTC
Created attachment 35247 [details]
Another seemingly viable option

Another option is to remove the -java-home option from the JSVC command lines. Doing so resolves the issue for me because JSVC seems to be able to figure out what to do on it's own.
Comment 3 Rainer Jung 2017-08-17 22:19:41 UTC
What about the following addition, copied and adjusted from the top of the file where it is used to resolve another possible symlink:

Index: bin/daemon.sh
--- bin/daemon.sh       (revision 1805340)
+++ bin/daemon.sh       (working copy)
@@ -94,6 +94,15 @@
 if [ -z "$JAVA_HOME" ]; then
     JAVA_BIN="`which java 2>/dev/null || type java 2>&1`"
+    while [ -h "$JAVA_BIN" ]; do
+        ls=`ls -ld "$JAVA_BIN"`
+        link=`expr "$ls" : '.*-> \(.*\)$'`
+        if expr "$link" : '/.*' > /dev/null; then
+            JAVA_BIN="$link"
+        else
+            JAVA_BIN="`dirname $JAVA_BIN`/$link"
+        fi
+    done
     test -x "$JAVA_BIN" && JAVA_HOME="`dirname $JAVA_BIN`"
     test ".$JAVA_HOME" != . && JAVA_HOME=`cd "$JAVA_HOME/.." >/dev/null; pwd`

Does that work?
Comment 4 Coty Sutherland 2017-08-18 12:06:10 UTC
Yeah, that works. I think I like that approach best also because it eliminates guessing.
Comment 5 Mark Thomas 2017-08-31 15:00:17 UTC
Fixed in:
- trunk for 9.0.0.M27 onwards
- 8.5.x for 8.5.21 onwards
- 8.0.x for 8.0.47 onwards
- 7.0.x for 7.0.82 onwards