Finally found the right configuration file my self. It is /etc/default/tomcat. There I was able to set
JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
and it works now.
Thanks for the help.
Answer from André Stannek on Stack ExchangeFinally found the right configuration file my self. It is /etc/default/tomcat. There I was able to set
JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
and it works now.
Thanks for the help.
export JAVA_HOME=/path/to/your/java/environment Then run tomcat.
You can do this in the users .bash_profile as well.
There are multiple ways of doing so.
Chaining global
JAVA_HOMEenvironment variables to the java you wish to use. Note that this will change Java for all apps running on that machine.Using setenv file. In
CATALINA_BASE/bindirectory open or create file namedsetenv.shand putJAVA_HOME=/usr/lib/jvm/openjdk-8-jdkin it. This will change it only for your tomcat. Please note to change it to the JDK/JRE you want to use.
Using the "setenv" script (optional, recommended)
Apart from CATALINA_HOME and CATALINA_BASE, all environment variables can be specified in the "setenv" script. The script is placed either into CATALINA_BASE/bin or into CATALINA_HOME/bin directory and is named setenv.bat (on Windows) or setenv.sh (on *nix). The file has to be readable.
By default the setenv script file is absent. If the script file is present both in CATALINA_BASE and in CATALINA_HOME, the one in CATALINA_BASE is preferred.
For example, to configure the JRE_HOME and CATALINA_PID variables you can create the following script file:
On *nix, $CATALINA_BASE/bin/setenv.sh:
JRE_HOME=/usr/java/latest CATALINA_PID="$CATALINA_BASE/tomcat.pid"
http://tomcat.apache.org/tomcat-9.0-doc/RUNNING.txt
You can try this:
Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command below.
gksudo gedit /etc/default/tomcat7
When the file opens, uncomment the line that sets the JAVA_HOME variable.

Save and restart tomcat7 server.
Tomcat will not actually use your JAVA_HOME environmente variable, but look in some predefined locations and in the JAVA_HOME variable set inside the startup script, as other answers point out. If you don't like messing with the tomcat startup script, you could create a symlink for your preferred java installation, which will be picked up by tomcat.
For example:
ln -s /usr/lib/jvm/java-8-oracle /usr/lib/jvm/default-java
When you open catalina.sh / catalina.bat, you can see :
Environment Variable Prequisites
JAVA_HOME Must point at your Java Development Kit installation.
So, set your environment variable JAVA_HOME to point to Java 6. Also make sure JRE_HOME is pointing to the same target, if it is set.
Update: since you are on Windows, see here for how to manage your environment variables
You can change the JDK or JRE location using the following steps:
- Open the terminal or cmd.
- Go to the
[tomcat-home]\bindirectory.
ex:c:\tomcat8\bin - Write the following
command:
Tomcat8W //ES//Tomcat8 - Dialog will open, select the Java tab (top pane).
- Change the Java Virtual Machine value.
- Click OK.
Note:
In Apache TomEE same steps, but step (3) the command must be: TomEE //ES
Even if you managed to run tomcat 9 with java 17, a web app using spring boot 3.x or spring framework 6.x would still not be able to run there.
Reason is that starting with spring boot 3.0 or spring framework 6.0 the web application needs to conform to jakarta servlet api. According to doc for tomcat the jakarta servlet api (servlet v5.0) is supported from tomcat 10 and later.
As described in doc
Apache Tomcat 10.0.x
Apache Tomcat 10.0.x builds on Tomcat 9.0.x and implements the Servlet 5.0, JSP 3.0, EL 4.0, WebSocket 2.0 and Authentication 2.0 specifications (the versions required by Jakarta EE 9 platform).
So you have to use tomcat 10 instead of tomcat 9.
Well I have found this thread after finding the answer so I will write my solution If it can help anyone :
Operating system : Ubuntu 22.04
Installing Java : apt-get install openjdk-17-jdk
Adding JAVA_HOME : nano /etc/environment
-> JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
Then
Installing Tomcat9 with : apt-get install tomcat9
Starting tomcat9 with : service tomcat9 start
An error is following stating JAVA_HOME is not found which is incorrect.
You need to find the script /usr/libexec/tomcat9/tomcat-locate-java.sh
Which actually seach for the versions 11 to 8 of Java.
Updating the script this way adding the version 17 is solving the issue :
find_jdks()
{
for java_version in 17 11 10 9 8
do
There is a help text in catalina.sh. I will quote it here:
# Do not set the variables in this script. Instead put them into a script
# setenv.sh in CATALINA_BASE/bin to keep your customizations separate.
#
# JAVA_HOME Must point at your Java Development Kit installation.
# Required to run the with the "debug" argument.
# Ensure that any user defined CLASSPATH variables are not used on startup,
# but allow them to be specified in setenv.sh, in rare case when it is needed.
CLASSPATH=
if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
. "$CATALINA_BASE/bin/setenv.sh"
elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
. "$CATALINA_HOME/bin/setenv.sh"
fi
When you starting tomcat using catalina.sh, it searching for file setenv.sh and sourcing it. It is searching in CATALINA_HOME or CATALINA_BASE.
So the better way to set JAVA_HOME for the tomcat is:
- Create a script named
setenv.shin the folderCATALINA_BASE/bin, if it does not exist already. Add this line to
setenv.shexport JAVA_HOME=/opt/java/jdk1.8.0_05Make it executable.
Why you should use this solution:
Setting environment variable in script is safer. Always try to set variables as locally as possible. Try do not use /etc/environment, /etc/profile and others if you really do not need Global Environment Variable. Setting JAVA_HOME in setenv.sh gives you ability to use different tomcats with different applications that need different version of java, but running by one user. Other user environment would not be affected by you.
Since you have set the environment variable for your own user and not for the superuser, you have two options:
You will have to export the variable using
-Eoption as follows:sudo -E /opt/tomcat7/apache-tomcat-7.0.53/bin/startup.shNote that this will export all environment variables while running the command. This is not preferred since the normal users environment is spilled out when you run the command as root. This is not desirable.
Export the variable in root's
.bashrc/etc/enviromentfile. Open a terminal and type:sudo nano /etc/environmentand enter your administrative password, and add the following lines to the end of the file:
JAVA_HOME=/opt/java/jdk1.8.0_05 PATH=$PATH:$JAVA_HOME/binand then
source /etc/environmentor restart your machine and then retry the command you were using.
Update:
This answer provided hints as two why step 2 wouldn't work, sudo would reset the environment and provide a secure path, so all global variables are reset. A workaround would be to use
sudo su
and then execute command which uses the set environment variables.