I have two Ubuntu 20.04 servers, Alpha and Beta, both with Tomcat 9 installed from the standard repo. In principle, they should be virtually identical, but I’ve found a discrepancy in which version of Java each uses that I can’t find the origin of.
On server Alpha, both the ps
process listing for Tomcat and systemctl status tomcat9.service
show that its Java binary is /usr/lib/jvm/java-8-openjdk-amd64/bin/java
. On server Beta, the same commands show that Tomcat’s Java binary is /usr/lib/jvm/default-java/bin/java
, which is a symlink to /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java
.
Thus, server Alpha uses Java 8, while server Beta uses Java 11. They should be identical, yet something is clearly different.
The obvious question at this point is, how does Tomcat determine which version of Java to use?
1) Documentation
I cannot find the answer to this question in the Tomcat configuration documentation, and there are no other top-level topics in the documentation that seem relevant to this question.
2) Stack Overflow
This Stack Overflow question is the best I can find on the site, and it doesn’t have any useful answers for an Ubuntu system; /etc/sysconfig/tomcat{N}
is mentioned in one answer, but it does not exist on either server, while /etc/default/tomcat{N}
exists on both but does not contain any parameters relating to Java version.
3) Standard config files
I have also checked the main Tomcat config file /var/lib/tomcat9/conf/server.xml
and the Tomcat systemd
service file /lib/systemd/system/tomcat9.service
, but neither contain any parameters relating to Java version. Both servers have a service file override /etc/systemd/system/tomcat9.service.d/override.conf
, but since I wrote that myself, I’m certain it doesn’t set the version of Java used.
4) setenv.sh
Tomcat’s /usr/share/tomcat9/bin/catalina.sh
file mentions a JAVA_HOME
environment variable, which I have to assume is what I’m looking for. That file appears to expect this variable to be set when it sources one of two files, $CATALINA_BASE/bin/setenv.sh
or $CATALINA_HOME/bin/setenv.sh
. Neither of these files (or bin/
directories, even) exist on either server, so this can’t explain the difference.
That’s all I can figure out. The question is:
In the absence of explicit user configuration, what determines what Java version Tomcat uses? This information must be stored in one of the files in Tomcat’s installation, but I have no way of knowing which one.
EDIT
As @Emerson Pardo suggested, the two instances of Ubuntu 20.04 did have different default Java versions. After changing Beta’s to use Java 8 using the Debian/Ubuntu update-alternatives
program and restarting Tomcat, however, the problem outlined above is unchanged. Tomcat continues to use
/usr/lib/jvm/default-java/bin/java -> /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java
on Beta.
2
Answers
Generally, Ubuntu chooses what Java it will be used by default. You can find it like this:
You can also check the alternatives available in your system using:
which will list all java installed.
To choose a specific version you can use:
On derivatives of Debian 10, such as Ubuntu 20.04, Tomcat is started through a wrapper script located in
/usr/libexec/tomcat9/tomcat-start.sh
. If you don’t setJAVA_HOME
yourself a heuristic procedure is performed in/usr/libexec/tomcat9/tomcat-locate-java.sh
:Therefore:
/usr/lib/jvm/default-java
exists it is chosen,