How to set JAVA_HOME for multiple Tomcat instances? - java

I have 2 Java Web Projects. One runs on JDK 1.5 and the other runs on JDK 1.6. I want to run both of them on the same computer, but the JAVA_HOME environment variable can only have one value. I want to set JAVA_HOME for each Tomcat server.

place a setenv.sh in the the bin directory with
JAVA_HOME=/usr/java/jdk1.6.0_43/
JRE_HOME=/usr/java/jdk1.6.0_43/jre
or an other version your running.

One thing that you could do would be to modify the catalina.sh (Unix based) or the catalina.bat (windows based).
Within each of the scripts you can set certain variables which only processes created under the shell will inherit. So for catalina.sh, use the following line:
export JAVA_HOME="intented java home"
And for windows use
set JAVA_HOME="intented java home"

If you are a Windows user, put the content below in a setenv.bat file that you must create in Tomcat bin directory.
set JAVA_HOME=C:\Program Files\Java\jdk1.6.x
If you are a Linux user, put the content below in a setenv.sh file that you must create in Tomcat bin directory.
JAVA_HOME=/usr/java/jdk1.6.x

Also, note that there shouldn't be any space after =:
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_27

You can add setenv.sh in the the bin directory with:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
and it will dynamically change when you update your packages.

In UNIX I had this problem, I edited catalina.sh manually and entered
export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.24
echo "Using JAVA_HOME: $JAVA_HOME"
as the first 2 lines. I tried setting the JAVA_HOME in /etc/profile but it did not help.
This worked finally.

I think this is a best practice (You may be have many Tomcat instance in same computer, you want per Tomcat instance use other Java Runtime Environment):
This is manual inside file: catalina.sh
# JRE_HOME Must point at your Java Runtime installation.
# Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME
# are both set, JRE_HOME is used.

Linux based Tomcat6 should have /etc/tomcat6/tomcat6.conf
# System-wide configuration file for tomcat6 services
# This will be sourced by tomcat6 and any secondary service
# Values will be overridden by service-specific configuration
# files in /etc/sysconfig
#
# Use this one to change default values for all services
# Change the service specific ones to affect only one service
# (see, for instance, /etc/sysconfig/tomcat6)
#
# Where your java installation lives
#JAVA_HOME="/usr/lib/jvm/java-1.5.0"
# Where your tomcat installation lives
CATALINA_BASE="/usr/share/tomcat6"
...

I had the same problem my OS is windows 8 and I am using Tomcat 8,
I just edited the setclasspath.bat file in bin folder and set JAVA_HOME and JRE_HOME
like this...
#echo off
...
...
set "JRE_HOME=%ProgramFiles%\Java\jre8"
set "JAVA_HOME=%ProgramFiles%\Java\jdk1.7.0_03"
...
...
and it works fine for me now......

For Debian distro we can override the setting via defaults
/etc/default/tomcat6
Set the JAVA_HOME pointing to the java version you want.
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64

Just a note...
If you add that code to setclasspath.bat or setclasspath.sh, it will actually be used by all of Tomcat's scripts you could run, rather than just Catalina.
The method for setting the variable is as the other's have described.

I think you only need add in startup.sh or startup.bat the set JAVA_HOME=C:\Program Files\Java\jdk-19
After check...

Related

Starting eclipse with configured JVM from windows batch file

I have problems to start eclipse with a proper configured JVM from windows batch file.
I like to do it this way, because I have different workspaces, different eclipse versions, different java versions and different configurations.
I guess the problem is the way I pass the parameters. I have tried several combinations of setting '"', but none worked. Below is just one example.
Anyway, can someone help?
Thanks :-)
Afterwards added:
Sorry, I forgot - the problem is the parameters for the JVM are neglected
#echo off
ver
echo.
set thePathToJDK=C:\Program Files\AdoptOpenJDK\jdk-11.0.10x9-hotspot
rem to be checked - is JAVA_HOME needed by anyone ?
rem eclipse states that is neglecting JAVA_HOME
set JAVA_HOME=%thePathToJDK%
rem set path to vm ( shall be in harmony with path to JDK )
set thePathToJVM=%thePathToJDK%\bin\javaw
set thePathToEclipse=C:\eclipse\ ...
set thePathToEclipseWorkSpace=C:\ ...
rem set minimal path (for ECLIPSE/JAVA)
set theNewPath=%SystemRoot%\system32
set theNewPath=%theNewPath%;%SystemRoot%
set theNewPath=%theNewPath%;%SystemRoot%\System32\Wbem
set theNewPath=%theNewPath%;%SystemRoot%\System32\WindowsPowerShell\v1.0\
set path=%thePathToJDK%\bin;%theNewPath%
echo.
echo path used for ECLIPSE
echo %path%
set theCommand=%thePathToEclipse%\eclipse.exe -data "%thePathToEclipseWorkSpace%" -vm "%thePathToJVM%" -vmargs -ea -Xmx4096m -Xss256m
echo.
echo %theCommand%
rem start eclipse with preconfigured Java
cmd /c %theCommand%
pause
On Windows, you need to include the .exe part of the VM's file name when you're using -vm with a file rather than a folder.
As #greg-449 pointed out, "the parameters" only change the Java that Eclipse itself uses.
What I wanted cannot be done the way I wanted it to be done
:-(

Why JAVA_HOME not recognized by tomcat7 in Ubuntu?

I have installed tomcat7 on my Ubuntu machine. When I try to restart the server I get message to set JAVA_HOME but it is set in .bashrc
export JAVA_HOME=/usr/lib/jvm/java-8-oracle
export CATALINA_HOME=/usr/share/tomcat7
Error:
omkars#<ubuntu_14.04>:~$ sudo service tomcat7 restart
[sudo] password for omkars:
* no JDK or JRE found - please set JAVA_HOME
omkars#<ubuntu_14.04>:~$ echo $JAVA_HOME
/usr/lib/jvm/java-8-oracle
What could be missing ?
Thanks.
Now, its working!
Changes I have done are:
changed .bashrc as explained in the question.
changed /etc/init.d/tomcat7 to point to oracle Java8 which is missing here!
JDK_DIRS="/usr/lib/jvm/default-java ${OPENJDKS} /usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-sun /usr/lib/jvm/java-7-oracle **/usr/lib/jvm/java-8-oracle**"
Then,
root#omkars-Dell-System-Inspiron-N4110:~# sudo service tomcat7 restart
* Starting Tomcat servlet engine tomcat7 [ OK ]
Got a hint from this page:
https://mifosforge.jira.com/wiki/display/MIFOSX/Install+Tomcat+7+on+Ubuntu+11.10+for+Mifos+X
Thanks
It seems like the preferred way of handling this is to uncomment the JAVA_HOME entry in /etc/default/tomcat7 and adjust the path accordingly. If you're using the webupd8 repository with the oracle-java8-installer, it's JAVA_HOME=/usr/lib/jvm/java-8-oracle.
It'll need to be set for the user that runs the tomcat service, rather than for your user.
Set it in the system wide profile, somewhere in /etc/profile or /etc/profile.d/, depending on how your machine is configured.
The startup script at /etc/init.d/tomcat7 sources the file /etc/default/rcS before searching for some well-known install locations.
Adding the line JAVA_HOME=/usr/lib/jvm/java-8-oracle to /etc/default/rcS corrects the no JDK or JRE found startup problem without directly modifying the /etc/init.d/tomcat7 script.
You can set an environmental variable in the setenv.sh script. According to the Running The Apache Tomcat 7.0 document:
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).
So just add the following line to setenv.sh:
export JAVA_HOME=/usr/lib/jvm/java-8-oracle
This way you are setting the variable locally.
I have that same problem but I solve it by changing
JDK_DIR variable in /etc/init.d/tomcat as follow :
JDK_DIRS="/usr/lib/jvm/default-java ${OPENJDKS} /usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-sun /usr/lib/jvm/java-7-oracle /usr/lib/jvm/java-8-oracle"
Try install Java using the repository of http://www.webupd8.org .
This is for Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html

'JAVA_HOME is not set' error while installing pig. What to do about it?

I followed all the steps on pig.apache.org ,but not able to remove this error by setting java variable. I set the variable earlier while installing java jdk but its asking again for the variable.
You need to understand how environment variables work in Linux (or Windows).
The chances are that you only set JAVA_HOME temporarily in the shell that you used to do the installation. To set JAVA_HOME permanently (on Linux / UNIX) you need to set it in a shell "rc" file that gets run each time a new shell is created. (It depends on which shell you are using, but man can tell you about that ...)
if you are installing PIG on ubuntu do this
open bashrc file using below command and and lines to the end of the file
vi ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-i386
then log out or Restart your system and try again.
For CentOS
Create a new file called java.sh
vim /etc/profile.d/java.sh
Within this file, initialize the necessary environment variables
export JRE_HOME=/usr/java/jdk1.5.0_12/jre
export PATH=$PATH:$JRE_HOME/bin
export JAVA_HOME=/usr/java/jdk1.5.0_12
export JAVA_PATH=$JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin
Replace java path with your java installation.
Source
To set your JAVA_HOME variable for your current shell, use the export command. This will immediately create an environment variable for JAVA_HOME, but the variable is lost when your shell is closed.
export JAVA_HOME=<path_to_java_sdk>
The best way to set the JAVA_HOME environment variable for Pig and Hadoop use is by adding it to your local rc file. The reason for this is that some distributions of Hadoop (Cloudera, Hortonworks, MapR) may include their own Java installation for the Hadoop application itself. You may not want to create a global environment variable, since it may interfere with Hadoop/Hive/etc.
To have the variable set for individual Hadoop/Pig users on the server, they need to run the following command:
echo "export JAVA_HOME=<path_to_java_sdk>" >> ~/.bashrc
Afterward executing the command, they can source ~/.bashrc to instantiate the environment variable.

default value of JAVA_HOME seems to be overwriting the value set by: /etc/profile.d/java.sh

I've created a file /etc/profile.d/java.sh that includes (among other things) the line of code: export JAVA_HOME=/usr/java/jdk1.6.0_30. However, when I log in via SSH or reboot the linux Centos 6.2 server, I always see:
[root#host3 /etc/profile.d]# echo $JAVA_HOME
/usr/local/jdk
This is a new installation and server, so there's not much else done. It seems that the java.sh file is executing upon reboot or SSH login, however, the value of JAVA_HOME is being overwritten by the default value of /usr/local/jdk somehow. There isn't even a directory jdk under /usr/local.
Any idea how to correct this? The value of JAVA_HOME needs to be set (as done in java.sh) for all users.
UPDATE 1:
I checked the /etc/profile file and searched for the text jdk as well as JAVA and neither were found. I also checked the /root/.bash_profile and it shows as follows:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
Also, if I source java.sh, everything gets set correctly. But if I reboot server, JAVA_HOME goes back to /usr/local/jdk.
Check the /etc/profile file or the .bash_profile/.profile in your home directory.
It appears that cPanel adds the following lines to the /etc/bashrc file to set the JAVA_HOME to it's default.
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/classes.zip
So, if you have cPanel installed, make sure you check this file as well.

How to change java_opts for tomcat when we run it as a windows service manually?

I'm manually running tomcat 6 as a windows service on the console. I need to change java_opts before starting it. How do I do that? Also, Is there a way I can see the logs dynamically?
I know this is an old thread but needed to correct some assumptions.
Just an FYI, Catalina.bat is not utilized when running tomcat as a service. here is the method to change the JAVA_OPTS for tomcat running as a windows service.
Open Services and click on the Tomcat service. Make a note of the service name (most likely Tomcat6).
cd to the Tomcat bin directory
Run the command
tomcat6w //ES//Tomcat6 (substitute your service name if different)
Click on the Java tab
Add the options (each on a new line) to the Java Options box and set the initial and max memory to 1536 and 2048
-XX:MaxPermSize=256m
-Djava.awt.headless=true
-Djava.net.preferIPv4Stack=true
Click apply
Click on the general tab and restart the service
To change the settings, create a file named setenv.bat for windows or setenv.sh for Linux with entry as below:
Windows:
set JAVA_OPTS="-Xms256m -Xmx512m"
Linux:
export JAVA_OPTS="-Xms256m -Xmx512m"
Simply put this(setenv.bat/setenv.sh) file in %CATALINA_HOME%\bin\ folder. Your command file (catalina.bat/catalina.sh) already has a statement as below:
Windows:
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
Linux:
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
This will take care the rest.
To alter the $JAVA_OPTS, you will probably need to edit the batch file you use to start Tomcat. I don't run Tomcat on Windows, but the $JAVA_OPTS appears in my catalina.sh inside the bin/ directory on my Linux installation.
As far as viewing logs dynamically on Windows, there are a couple of options I'm aware of.
Download and install Cygwin, and then on the command-line, use tail -f logfilename like you would in Linux.
Get the BearTail program and use that to follow your log files.
You can set your JAVA_OPTS environment variable either manually via the command line prior to starting Tomcat:
set JAVA_OPTS=youropts
or you can edit catalina.bat with the values you want.
In windows, cut the quotes on the set command. Quotes are taken literally.

Categories