Tomcat and CATALINA_HOME - java

I am messing with Tomcat server and I think some strange thing happened. I downloaded Tomcat 7 from apache website (not an RPM but tar.gz). As I understand there should be CATALINA_HOME and CATALINA_BASE environment variables set, but when I do export on my terminal I can't see any of them, but server starts fine through bin/startup.sh and it even says
Using CATALINA_BASE: /home/andrzej/apache-tomcat-7.0.25
Using CATALINA_HOME: /home/andrzej/apache-tomcat-7.0.25
Using CATALINA_TMPDIR: /home/andrzej/apache-tomcat-7.0.25/temp
Using JRE_HOME: /usr/lib/jvm/jre
Using CLASSPATH: /home/andrzej/apache-tomcat-7.0.25/bin/bootstrap.jar:/home/andrzej /apache-tomcat-7.0.25/bin/tomcat-juli.jar
I think that the other day variables were set, but now I tried to use them with keytool to generate certificate and I noticed they are not there.
Am I missing something, because even if I can't see them everything works fine (startup/shutdown and eclipse with tomcat)?

Those environment variables are optional. If there are none set on the system, Tomcat will just set some defaults based on the location of the currently running script (the startup.sh and so on), which will be used as long as Tomcat is running. If you know Shell script language, just open catalina.sh file in an editor and read it.

If you take a look into catalina.sh (which is called from startup.sh) you will notice that in case CATALINA_HOME is not set it automatically sets it to one level above of the program executable:
[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." ; pwd`
and CATALINA_BASE, if absent, is set equal to CATALINA_HOME:
if [ -z "$CATALINA_BASE" ] ; then
CATALINA_BASE="$CATALINA_HOME"
fi
So there's no real requirement for the variables to be set.

Related

my tomcat uses wrong JAVA_HOME or JRE_HOME

I'm new to Tomcat. their is something wrong about Tomcat 7 on my ubuntu 10.10.
when I start Tomcat like this
llchli#llchli-virtual-machine:/usr/local/apache-tomcat-7.0.47/bin$ ./catalina.sh run
Using CATALINA_BASE: /usr/local/apache-tomcat-7.0.47
Using CATALINA_HOME: /usr/local/apache-tomcat-7.0.47
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.47/temp
Using JRE_HOME: /usr/local/jdk1.7.0_45
Using CLASSPATH: /usr/local/apache-tomcat-7.0.47/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.47/bin/tomcat-juli.jar
although cannot start Tomcat this way, it echos Using JRE_Home /usr/local/jdk1.7.0_45, which is the correct JAVA_HOME or JRE_HOME on my computer.
but when I start Tomcat with sudo like this
llchli#llchli-virtual-machine:/usr/local/apache-tomcat-7.0.47/bin$ sudo ./catalina.sh start
[sudo] password for llchli:
Using CATALINA_BASE: /usr/local/apache-tomcat-7.0.47
Using CATALINA_HOME: /usr/local/apache-tomcat-7.0.47
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.47/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-7.0.47/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.47/bin/tomcat-juli.jar
it echos Using JRE_HOME: /usr,which is not the correct JAVA_HOME or JRE_HOME on my computer. But it start Tomcat correctly.
my /etc/environment is
llchli#llchli-virtual-machine:/usr/local/apache-tomcat-7.0.47/bin$ sudo cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/jdk1.7.0_45/bin"
LANGUAGE="en"
JAVA_HOME="/usr/local/jdk1.7.0_45"
CLASSPATH="$CLASSPATH:$JAVA_HOME/lib"
CATALINA_HOME="/usr/local/apache-tomcat-7.0.47"
I'm a bit puzzled about this.And when I use daemon.sh like this, it generate errors.
llchli#llchli-virtual-machine:/usr/local/apache-tomcat-7.0.47/bin$ sudo ./daemon.sh run
Cannot find any VM in Java Home /usr
Cannot find any VM in Java Home /usr
Cannot locate JVM library file
Service exit with a return value of 1
It echos cannot find any VM in Java Home /usr. I'm so confused.
Any helps and advise will appreciate.
Thanks in advance.
It sounds like "sudo" is not passing your JAVA_HOME and JAVA_PATH through to the child process.
When you run a command using "sudo" the environment variables are set according to what the sudoers file says. My reading of the "sudoers" manual entry is that the default is to NOT pass variables through. But according to the "sudo" manual entry, there is a command syntax variation that allows you to pass environment variables using "VAR=value" ...
I suggest that you read the manual entries for "sudo" and "sudoers" carefully, and then read your system's "/etc/sudoers" file to understand how it is behaving.
Having said that, launching services from the command line using "sudo" is not a good idea, especially if you are talking about "production" services. And you are generally better off using the service wrappers provided by your distro's packaging, rather than a non-packaged install from the "upstream" project.
Sudo resets the environment variables. When you run sudo, JRE_HOME is reset to its default (probably null) and Tomcat will assume its own default (/usr). You can run sudo with the -E switch to preserve the environment.
Are you sure, you must run the script as root? I have no idea how to set up Tomcat, but it is usually a good practice to run servers as a dedicated user instead of as root.
As i already state in a comment, i strongly suggest you to use the package system to install software, it it's available in the repository. (plus there are ppa's for nearly everything). That way you will keep your system tidy and you won't have worry about things not working (usually).
My suggestion is to remove your current manual install and install the tomcat7 package.
apt-get install tomcat7
For further configuration, use the file /etc/default/tomcat7. To start/stop/restart use the command service tomcat7 {start|stop|restart}.
Using the package tomcat7 the tomcat instance will run as it's own user, and not as root, and that's good, since a service should not run as a user with full privileges.
I am aware that i am actually not answering your question with the environment variables. But using the suggested way, you will not face that problem to begin with.
it is all said, it means adding to /etc/sudoers a line with:
Defaults env_keep += "JAVA_HOME JRE_HOME"

In which file Tomcat Java Memory options are saved

I want to change memory configuration of Tomcat. I know it can be done at
Start > Apache Tomcat > Configure Tomcat > Java Tab] as can be seen below
and specifying options like
-Xms512m
-Xmx1024m
-XX:MaxPermSize=512m
-XX:MaxHeapFreeRatio=70
there.
However this program group is created if I install Tomcat from an executable. I have installed Tomcat by simply extracting the zip which does not create Program group. So now where can I specify this information
With regards to the question of where Tomcat's memory options are saved when using the service version, on my machine at least they can be found in the Windows Registry at:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\Tomcat-6\Parameters\Java
Initial memory and Maximum memory are recorded against the JvmMs and JvmMx values, while the options text is recorded against the Options value.
The Tomcat-6 portion of the address on mine is, I believe, based on the identifier I gave to the service (i.e. within the service manager Tomcat is named "Apache Tomcat 6.0 Tomcat-6"), and you may not need to go through Wow6432Node depending on whether you're using 32-bit or 64-bit Tomcat.
I'm assuming here that they haven't made any dramatic changes in where this information is stored in versions of Tomcat more recent than Tomcat 6.
Ohkay while digging inside Tomcat I found some information in one of its txt files (point 3.4) and online:
Apart from CATALINA_HOME (which is set to apache-tomcat-x.x.x directory, for e.g. C:\Program Files\Apache Software Foundation\apache-tomcat-7.0.41) and CATALINA_BASE (optional, defaults to CATALINA_HOME), 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 Windows, %CATALINA_BASE%\bin\setenv.bat:
set "JRE_HOME=%ProgramFiles%\Java\jre6"
exit /b 0
On *nix, $CATALINA_BASE/bin/setenv.sh:
JRE_HOME=/usr/java/latest
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
To set Java options for memory allocation for web app, add something like following (as per memory requirements of your application) to the same file
set JAVA_OPTS="-Xms256m -Xmx512m"
The CATALINA_HOME and CATALINA_BASE variables cannot be configured in the
setenv script, because they are used to locate that file.
All the environment variables described here and the setenv script are
used only if you use the standard scripts to launch Tomcat. For example, if
you have installed Tomcat as a service on Windows, the service wrapper
launches Java directly and does not use the script files.

How to pass the -D additional parameter while starting tomcat?

I have a maven project, after running command mvn install all project as well as module compile and copied to local repository successfully. But now I want to run the generated web application in tomcat6. Client provided some parameter for tomcat like -Dapp.username,-Dapp.username, which will be used internally once project will start.ButI do not know how to set these additional parameter in tomcat6. Below is my development environment
OS = Windows
Tomcat = tomcat 6.0.27
Please help me?
For Tomcat 6 you should add the params to the startup.sh (Windows startup.bat). For Tomcat 7 and above you should set the parameters in the {Catalina Root}/bin/setenv.sh like such:
export CATALINA_OPTS="$CATALINA_OPTS -Dapp.username=username -Dapp.password=password"
Or in Windows:
set CATALINA_OPTS="$CATALINA_OPTS -Dapp.username=username -Dapp.password=password"
NOTE: Notice the $CATALINA_OPTS at the beginning so you don't wipe out any previously set values. Not doing so can create a very hard to debug problem!
If the parameters you are setting are solely to be used by Tomcat then be sure to set it using CATALINA_OPTS.
If your application will be using the parameters then be sure to use JAVA_OPTS instead. Tomcat will also read these parameters. This can also go in the setenv.sh file. For instance:
export JAVA_OPTS="$JAVA_OPTS -Dapp.username=username -Dapp.password=password"
Or in Windows:
set JAVA_OPTS="$JAVA_OPTS -Dapp.username=username -Dapp.password=password"
You can set an environment variable to do that. E.g. in Linux:
export JAVA_OPTS="-Dapp.username -Dapp.username"
Or in Windows:
set JAVA_OPTS="-Dapp.username -Dapp.username"
Do this before starting Tomcat
You will want to set the CATALINA_OPTS system variable - this is read by Tomcat (and only by Tomcat) when starting. As #Betoverse says you can set this using the two methods:
export CATALINA_OPTS="-Dapp.username -Dapp.username"
Or in Windows:
set CATALINA_OPTS="-Dapp.username -Dapp.username"
You can add that command to your ~/.profile on UNIX to have it set automatically.
I have tested params for Tomcat 7/8 on Windows 10 and CentOs 7 (Linux).
1) On Windows need to create setenv.bat in the {TOMCAT_HOME}/bin/ path and insert there such code:
set CATALINA_OPTS=-Dapp.username=admin -Dapp.password=12345
IMPORTANT: do not use quotes (" ") for setting params on windows.
2) On CentOs need to create setenv.sh in the {TOMCAT_HOME}/bin/ path and insert there such code:
export CATALINA_OPTS="-Dapp.username=admin -Dapp.password=12345"
You can also create {TOMCAT_HOME}/conf/conf.d/custom.conf and insert the same export command there.
If you don't want to change your environments or edit the .sh files you can start the server with something like the following
CATALINA_OPTS="-Dparam1=value1 -Dparam2=value2" catalina.sh start
before starting tomcat server right click project --> Run as --> Run Configurations
second tab --> -Dname=values , -Dname=values , -Dname=values
what about +Dname=value ,, values it become encrypted

Using Tomcat 7 on Ubuntu 12.10 --- How To?

I am a newbie to Ubuntu 12.10 and moved to it from Windows.
In Windows I have configured the environment variable to include servlet-api.jar in the CLASSPATH variable so I do not have to type -cp <path to servlet-api.jar> every time I compile.
For normal Java programs, I have set the JAVA_HOME using:
export JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386" >> ~/.bashrc
With that solved, I need to set the CLASSPATH but do not know how to.
Please help on that!
Also I read on some forums that I should change the CLASSPATH manually each time I compile because changing the CLASSPATH may upset other applications.
Next, I tried this command to start Tomcat
service tomcat7 start but I got an error :
You need root privileges to run this script I do not know why. Help me solve that!
And if within the scope, please tell me how the directory structure of tomcat in Windows differs from that in Ubuntu.
Misc
I used sudo apt-get install tomcat7 to get Tomcat 7.
You don't say whether you're using an Ubuntu Tomcat package or a standalone Tomcat installation.
For a standalone Tomcat, you will be starting and stopping it using the scripts in its bin directory: startup.sh and shutdown.sh. In that case, Tomcat will use the environment variable values set at the time of invocation.
When doing this, you can set the environment variables prior to running these commands.
The same is true for the Tomcat 6 or Tomcat 7 that Ubuntu distributes as packages.
These neatly separate the $CATALINA_HOME directory (where the Tomcat 7 distribution is supposed to be) from the $CATALINA_BASE directory (where all of the changes and additions for a particular Tomcat instance are supposed to be. For the tomcat7 package, the first directory is /usr/share/tomcat7, while the second is /var/lib/tomcat7.
You can find startup.sh and shutdown.sh in /usr/share/tomcat7/bin and of course you can use them.
However, if you use the Tomcat provided with Ubuntu, you're probably going to want to run it just like any other standard system service, and the packages support this: e.g. in the case of tomcat7, you can just use
sudo service tomcat7 status
sudo service tomcat7 start
sudo service tomcat7 stop
sudo service tomcat7 restart
just like you can for any system service, and it uses the same mechanism as other system services do:
the shell script /etc/init.d/tomcat7 is used to start and stop Tomcat 7
/etc/default/tomcat7 is its configuration file, allowing a few parameters to be set
Using this method, you cannot specify additional environment variables for Tomcat to use by setting them in your shell or in /etc/default/tomcat7; they will not be passed to Tomcat.
However, there is a second method to set environment variables: if you set them in the shell script /var/lib/tomcat7/bin/setenv.sh (or, if you must, /usr/share/tomcat7/bin/setenv.sh), they will be picked up and passed to Tomcat. This method always works.
Finally, Tomcat offers finer control than just using a $CLASSPATH when it comes to specifying additional classes or JARs to be loaded on startup: in its conf/catalina.properties configuration file, you can set the common.loader, server.loader and shared.loader to a list of directories and/or JAR files to be loaded in additional to the standard ones.
All it means is that you should do that as a superuser - which translates to administrator in windows so you should try something like sudo service tomcat7 start
Try export CLASSPATH=/usr/share/tomcat7/lib/servlet-api.jar:/usr/share/tomcat7/lib/jsp-api.jar - This will depend on where your tomcat installation in located.
You can get more here
Cheers

Tomcat 7 setenv.sh is not found

I downloaded and extracted the apache-tomcat-7.0. As per the instructions in the RUNNING.txt (%CATALINA_BASE%/RUNNING.txt), it should set the JRE_HOME in the "setenv.sh" file.
Where is this file located ? Documentation said, it would be in CATALINA_HOME/bin directory. However this file is not present there.
Documentation does mention about the absence of setenv.(sh|bat) file:
(3.4) 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 Windows, %CATALINA_BASE%\bin\setenv.bat:
set "JRE_HOME=%ProgramFiles%\Java\jre6"
exit /b 0
On *nix, $CATALINA_BASE/bin/setenv.sh:
JRE_HOME=/usr/java/latest
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
http://tomcat.apache.org/tomcat-7.0-doc/RUNNING.txt
Just create one yourself; it isn't part of the distribution. It's not that hard. For your case, simply add
JRE_HOME=/path/to/your/java/installation
to the file and make it executable (chmod 755 setenv.sh).
You can also add other options (e.g. -Xmx) if necessary.
If you don't find the "setenv.sh" or "setenv.bat" in bin folder of tomcat, follow the following setps:
Create new file in bin folder of tomcat.
Rename it to setenv.sh for linux user or setenv.bat for windows user
Now you can set multiple thing into this file:
Setting JRE_HOME
For linux user
JRE_HOME=/path/to/jre/jre6
For Windows user
set JAVA_HOME=C:\Path\to\jre\jdk6
Setting JAVA_OPTS
For linux user
JAVA_OPTS="$JAVA_OPTS -Xms512m -Xmx512m -DFOOBAR_CONFIGURATION_FILE=file:///C:/foobar.properties"
For windows user
set "JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx512m -DFOOBAR_CONFIGURATION_FILE=file:///D:\foobar.properties"
Restart tomcat after setting variables.
That's it.
User Mindas' answer is completely correct: it is necessary to create the setenv.sh file in either the CATALINA_HOME or CATALINA_BASE directory, as the comment in the catalina.sh file indicates.
However, there is a "chicken and egg" problem wherein Tomcat must guess the location of the CATALINA_HOME/bin/ directory in the case where the CATALINA_HOME environment variable is not set.
I have encountered this situation on a Ubuntu server where fortunately Tomcat guesses the CATALINA_HOME/bin/ directory correctly. For a single-user environment where customization of Tomcat for different users isn't necessary, it seems that setting the JAVA_HOME or JRE_HOME environment variable in the catalina.sh script would be a solution to the "chicken and egg" problem.
In my case, I downloaded Tomcat for Windows. It also came with Linux shell scripts, so I created a 'setenv.sh' and tried to run it from Windows Subsystem for Linux, but got the above error. The issue was that the non-binary shell scripts were formatted for Windows, and Linux didn't know how to run them. After converting them to Linux format, it was able to recognize and run the scripts. Try this to convert from Windows to Linux:
dos2unix $CATALINA_HOME/bin/*.sh

Categories

Resources