tomcat 5 - how to change source to 1.5?

Hi.

I've got Tomcat5 running as a service in Linux. Where can I specify that I want tomcat to use Java 1.5 and not 1.3?

I'm having trouble because my servlet uses for-each loops, and so gives errors like this:

/usr/share/tomcat5/work/Catalina/localhost/XX/org/apache/jsp/index_jsp.java:63: for-each loops are not supported in -source 1.3

(try -source 1.5 to enable for-each loops)

for (String sp: split){

^

1 error

Any advice is much appreciated.

[597 byte] By [amaca] at [2007-11-27 5:55:40]
# 1

I have my installation path for Tomcat as "/usr/local/apache-tomcat-5.5.20" and from there I have a softlink to just "tomcat". I'll refer the paths to the softlink.

Edit/append the JAVA_HOME variable to /usr/local/tomcat/bin/catalina.sh.

Something like this:

JAVA_HOME="/usr/local/jdk1.5.0_10"

export JAVA_HOME

I'd put these somewhere in the beginning of catalina.sh

kaderuda at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thanks, but JAVA_HOME is set to point to a Java 1.5 installation. Tomcat seems to be set to 1.3 in spite of this.
amaca at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Even though you set the JAVA_HOME variable in the catalina.sh? That shouldn't happen, since the catalina.sh is the start/stop wrapper for Tomcat, and there is a bunch of other variables that you may set in catalina.sh to finetune the operation of Tomcat, for instance JAVA_OPTS, to set memory limits.

When I'm referring to the JAVA_HOME variable, I'm not talking about the system (shell) variable, but the Tomcat-file "catalina.sh", just to clarify.

kaderuda at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

There is no catalina.sh in this installation. I don't know whether this is down to someone else removing it, or a special kind of installation, but tomcat runs from starting '/etc/rc.d/init.d/tomcat5'. This file sets JAVA_HOME to the location of a Java 1.5 install, so I can't see how it reverts to 1.3 for tomcat.

amaca at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Do you have a "/usr/share/tomcat5/bin" directory then? If so, what are the names of the files therein?I think there is some tomcat specific .conf/.sh file which reverts to the old JDK.
kaderuda at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Folder contents:

ls -a

-rw-r--r-- 1 root root 30231 May 10 2005 bootstrap.jar

-rwxr-xr-x 1 root root713 May 10 2005 relink

-rwxr-xr-x 1 root root341 May 10 2005 reloctomcat5

#################################

Relink is:

#!/bin/sh

# Small script that will refresh all tomcat jar repositories

# (c) Nicolas Mailhot, JPackage project 2003

# $Id: tomcat5-5.0.relink,v 1.1.2.4.2.1 2004/12/21 15:00:28 jpackage Exp $

# Load JAVA_HOME

. /etc/tomcat5/tomcat5.conf

[ -z "$JAVA_HOME" ] && JAVA_HOME=/usr/lib/jvm/java

export JAVA_HOME

# Relink tomcat jar repositories

for repository in /var/lib/tomcat5/common/lib /var/lib/tomcat5/common/endorsed \

/var/lib/tomcat5/server/lib /var/lib/tomcat5/shared/lib ; do

rebuild-jar-repository $repository

done

for webapp in $(find /var/lib/tomcat5/webapps -type d -mindepth 1 -maxdepth 2) ; do

[ -d "$webapp/WEB-INF/lib" ] && rebuild-jar-repository $webapp/WEB-INF/lib

done

###################################

Reloc is:

#!/bin/sh

#

echo "relocating http & ajp ports to 81xx"

sed -e 's;8080;8180;'-e 's;8081;8181;'-e 's;8082;8182;'-e 's;8443;8543;'-e 's;8009;8109;'/etc/tomcat5/

server.xml > /etc/tomcat5/server.xml.reloc

mv /etc/tomcat5/server.xml /etc/tomcat5/server.xml.bak

cp /etc/tomcat5/server.xml.reloc /etc/tomcat5/server.xml

Neither looks particuarly relevant to me, but I could be wrong.

amaca at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

I'm not sure about this one:[ -z "$JAVA_HOME" ] && JAVA_HOME=/usr/lib/jvm/java

export JAVA_HOME

If JAVA_HOME is empty, it points to /usr/lib/jvm/java (what version is that?)

But I'm not sure that's it either. I don't see anything that even looks like it is booting up Tomcat itself.

There must be configuration somewhere else. What kind of *nix distro is this?

kaderuda at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Yes, it points to a Java 1.5 install.

It is fedora - though i couldn't tell you what version.

The startup script is in '/usr/bin/dtomcat5'. It looks pretty standard, but if you ctrl-f for ' # Search for java in PATH', this is the one bit where something weird may be happening.

#!/bin/sh

# --

# Start/Stop Script for the CATALINA Server

#

# Environment Variable Prequisites

#

#CATALINA_HOMEMay point at your Catalina "build" directory.

#

#CATALINA_BASE(Optional) Base directory for resolving dynamic portions

#of a Catalina installation. If not present, resolves to

#the same directory that CATALINA_HOME points to.

#

#CATALINA_OPTS(Optional) Java runtime options used when the "start",

#"stop", or "run" command is executed.

#

#CATALINA_TMPDIR (Optional) Directory path location of temporary directory

#the JVM should use (java.io.tmpdir). Defaults to

#$CATALINA_BASE/temp.

#

#JAVA_HOMEMust point at your Java Development Kit installation.

#

#JAVA_OPTS(Optional) Java runtime options used when the "start",

#"stop", or "run" command is executed.

#

#JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start"

#command is executed. The default is "dt_socket".

#

#JPDA_ADDRESS(Optional) Java runtime options used when the "jpda start"

#command is executed. The default is 8000.

#

#JSSE_HOME(Optional) May point at your Java Secure Sockets Extension

#(JSSE) installation, whose JAR files will be added to the

#system class path used to start Tomcat.

#

#CATALINA_PID(Optional) Path of the file which should contains the pid

#of catalina startup java process, when start (fork) is used

#

# $Id: catalina.sh,v 1.13.2.2 2004/11/17 20:06:34 yoavs Exp $

# --

# OS specific support. $var _must_ be set to either true or false.

cygwin=false

os400=false

case "`uname`" in

CYGWIN*) cygwin=true;;

OS400*) os400=true;;

esac

TOMCAT_CFG="/etc/tomcat5/tomcat5.conf"

JAVADIR="/usr/share/java"

# resolve links - $0 may be a softlink

PRG="$0"

while [ -h "$PRG" ]; do

ls=`ls -ld "$PRG"`

link=`expr "$ls" : '.*-> \(.*\)$'`

if expr "$link" : '.*/.*' > /dev/null; then

PRG="$link"

else

PRG=`dirname "$PRG"`/"$link"

fi

done

# Get standard environment variables

PRGDIR=`dirname "$PRG"`

# Only set CATALINA_HOME if not already set

[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." ; pwd`

if [ -r "$CATALINA_HOME"/bin/setenv.sh ]; then

. "$CATALINA_HOME"/bin/setenv.sh

fi

# For Cygwin, ensure paths are in UNIX format before anything is touched

if $cygwin; then

[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`

[ -n "$CATALINA_HOME" ] && CATALINA_HOME=`cygpath --unix "$CATALINA_HOME"`

[ -n "$CATALINA_BASE" ] && CATALINA_BASE=`cygpath --unix "$CATALINA_BASE"`

[ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`

[ -n "$JSSE_HOME" ] && JSSE_HOME=`cygpath --absolute --unix "$JSSE_HOME"`

fi

# For OS400

if $os400; then

# Set job priority to standard for interactive (interactive - 6) by using

# the interactive priority - 6, the helper threads that respond to requests

# will be running at the same priority as interactive jobs.

COMMAND='chgjob job('$JOBNAME') runpty(6)'

system $COMMAND

# Enable multi threading

export QIBM_MULTI_THREADED=Y

fi

[ -r "$TOMCAT_CFG" ] && . "${TOMCAT_CFG}"

### Set up defaults if they were omitted in TOMCAT_CFG

### JVM lookup

if [ -z "$JAVA_HOME" ]; then

# Search for java in PATH

JAVA=`which java`

if [ -z "$JAVA" ] ; then

JAVA_BINDIR=`dirname ${JAVA}`

JAVA_HOME="${JAVA_BINDIR}/.."

fi

# Default clean JAVA_HOME

[ -z "$JAVA_HOME" -a -d "/usr/lib/java" ] && JAVA_HOME="/usr/lib/java"

# Default IBM JAVA_HOME

[ -z "$JAVA_HOME" -a -d "/opt/IBMJava2-13" ] && JAVA_HOME="/opt/IBMJava2-13"

[ -z "$JAVA_HOME" -a -d "/opt/IBMJava2-131" ] && JAVA_HOME="/opt/IBMJava2-131"

[ -z "$JAVA_HOME" -a -d "/opt/IBMJava2-14" ] && JAVA_HOME="/opt/IBMJava2-14"

[ -z "$JAVA_HOME" -a -d "/opt/IBMJava2-141" ] && JAVA_HOME="/opt/IBMJava2-141"

# Another solution

[ -z "$JAVA_HOME" -a -d "/usr/java/jdk" ] && JAVA_HOME="/usr/java/jdk"

# madeinlinux JAVA_HOME

[ -z "$JAVA_HOME" -a -d "/usr/local/jdk1.2.2" ] && JAVA_HOME="/usr/local/jdk1.2.2"

# Kondara JAVA_HOME

[ -z "$JAVA_HOME" -a -d "/usr/lib/java/jdk1.2.2" ] && JAVA_HOME="/usr/lib/java/jdk1.2.2"

# Other commonly found JAVA_HOMEs

[ -z "$JAVA_HOME" -a -d "/usr/jdk1.2" ] && JAVA_HOME="/usr/jdk1.2"

# Default Caldera JAVA_HOME

[ -z "$JAVA_HOME" -a -d "/opt/java-1.3" ] && JAVA_HOME="/opt/java-1.3"

# Add other locations here

if [ -z "$JAVA_HOME" ]; then

echo "No JAVA_HOME specified in ${TOMCAT_CFG} and no java found, exiting..."

exit 1

else

echo "Found JAVA_HOME: ${JAVA_HOME}"

echo "Please complete your ${TOMCAT_CFG} so we won't have to look for it next time"

fi

fi

# Set standard commands for invoking Java.

_RUNJAVA="$JAVA_HOME"/bin/java

_RUNJAVAC="$JAVA_HOME"/bin/javac

_RUNJDB="$JAVA_HOME"/bin/jdb

# Set standard CLASSPATH

CLASSPATH="$JAVA_HOME"/lib/tools.jar

# Add on extra jar files to CLASSPATH

if [ -n "$JSSE_HOME" ]; then

CLASSPATH="$CLASSPATH":"$JSSE_HOME"/lib/jcert.jar:"$JSSE_HOME"/lib/jnet.jar:"$JSSE_HOME"/lib/jsse.jar

fi

# JPackage JSSE location check

if [ -r "$JAVADIR/jsse/jcert.jar" ]; then

CLASSPATH="$CLASSPATH":"$JAVADIR"/jsse/jcert.jar:"$JAVADIR"/jsse/jnet.jar:"$JAVADIR"/jsse/jsse.jar

fi

CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/bin/bootstrap.jar:`/usr/bin/build-classpath commons-logging-api mx4j/mx4j`

if [ -z "$CATALINA_BASE" ] ; then

CATALINA_BASE="$CATALINA_HOME"

fi

if [ -z "$CATALINA_TMPDIR" ] ; then

# Define the java.io.tmpdir to use for Catalina

CATALINA_TMPDIR="$CATALINA_BASE"/temp

fi

if [ -z "$CATALINA_PID" ] ; then

export CATALINA_PID=/var/run/tomcat5.pid

fi

# For Cygwin, switch paths to Windows format before running java

if $cygwin; then

JAVA_HOME=`cygpath --absolute --windows "$JAVA_HOME"`

CATALINA_HOME=`cygpath --absolute --windows "$CATALINA_HOME"`

CATALINA_BASE=`cygpath --absolute --windows "$CATALINA_BASE"`

CATALINA_TMPDIR=`cygpath --absolute --windows "$CATALINA_TMPDIR"`

CLASSPATH=`cygpath --path --windows "$CLASSPATH"`

[ -n "$JSSE_HOME" ] && JSSE_HOME=`cygpath --absolute --windows "$JSSE_HOME"`

JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"`

fi

# -- Execute The Requested Command --

echo "Using CATALINA_BASE:$CATALINA_BASE"

echo "Using CATALINA_HOME:$CATALINA_HOME"

echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR"

echo "Using JAVA_HOME:$JAVA_HOME"

if [ "$1" = "jpda" ] ; then

if [ -z "$JPDA_TRANSPORT" ]; then

JPDA_TRANSPORT="dt_socket"

fi

if [ -z "$JPDA_ADDRESS" ]; then

JPDA_ADDRESS="8000"

fi

if [ -z "$JPDA_OPTS" ]; then

JPDA_OPTS="-Xdebug -Xrunjdwp:transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=n"

fi

CATALINA_OPTS="$CATALINA_OPTS $JPDA_OPTS"

shift

fi

if [ "$1" = "debug" ] ; then

if $os400; then

echo "Debug command not available on OS400"

exit 1

else

shift

if [ "$1" = "-security" ] ; then

echo "Using Security Manager"

shift

exec "$_RUNJDB" $JAVA_OPTS $CATALINA_OPTS \

-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \

-sourcepath "$CATALINA_HOME"/../../jakarta-tomcat-catalina/catalina/src/share \

-Djava.security.manager \

-Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \

-Dcatalina.base="$CATALINA_BASE" \

-Dcatalina.home="$CATALINA_HOME" \

-Djava.io.tmpdir="$CATALINA_TMPDIR" \

org.apache.catalina.startup.Bootstrap "$@" start

else

exec "$_RUNJDB" $JAVA_OPTS $CATALINA_OPTS \

-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \

-sourcepath "$CATALINA_HOME"/../../jakarta-tomcat-catalina/catalina/src/share \

-Dcatalina.base="$CATALINA_BASE" \

-Dcatalina.home="$CATALINA_HOME" \

-Djava.io.tmpdir="$CATALINA_TMPDIR" \

org.apache.catalina.startup.Bootstrap "$@" start

fi

fi

elif [ "$1" = "run" ]; then

shift

if [ "$1" = "-security" ] ; then

echo "Using Security Manager"

shift

exec "$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \

-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \

-Djava.security.manager \

-Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \

-Dcatalina.base="$CATALINA_BASE" \

-Dcatalina.home="$CATALINA_HOME" \

-Djava.io.tmpdir="$CATALINA_TMPDIR" \

org.apache.catalina.startup.Bootstrap "$@" start

else

exec "$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \

-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \

-Dcatalina.base="$CATALINA_BASE" \

-Dcatalina.home="$CATALINA_HOME" \

-Djava.io.tmpdir="$CATALINA_TMPDIR" \

org.apache.catalina.startup.Bootstrap "$@" start

fi

elif [ "$1" = "start" ] ; then

shift

touch "$CATALINA_BASE"/logs/catalina.out

if [ "$1" = "-security" ] ; then

echo "Using Security Manager"

shift

"$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \

-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \

-Djava.security.manager \

-Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \

-Dcatalina.base="$CATALINA_BASE" \

-Dcatalina.home="$CATALINA_HOME" \

-Djava.io.tmpdir="$CATALINA_TMPDIR" \

org.apache.catalina.startup.Bootstrap "$@" start \

>> "$CATALINA_BASE"/logs/catalina.out 2>&1 &

if [ ! -z "$CATALINA_PID" ]; then

echo $! > $CATALINA_PID

fi

else

"$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \

-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \

-Dcatalina.base="$CATALINA_BASE" \

-Dcatalina.home="$CATALINA_HOME" \

-Djava.io.tmpdir="$CATALINA_TMPDIR" \

org.apache.catalina.startup.Bootstrap "$@" start \

>> "$CATALINA_BASE"/logs/catalina.out 2>&1 &

if [ ! -z "$CATALINA_PID" ]; then

echo $! > $CATALINA_PID

fi

fi

elif [ "$1" = "stop" ] ; then

shift

FORCE=0

if [ "$1" = "-force" ]; then

shift

FORCE=1

fi

"$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \

-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \

-Dcatalina.base="$CATALINA_BASE" \

-Dcatalina.home="$CATALINA_HOME" \

-Djava.io.tmpdir="$CATALINA_TMPDIR" \

org.apache.catalina.startup.Bootstrap "$@" stop

if [ $FORCE -eq 1 ]; then

if [ ! -z "$CATALINA_PID" ]; then

echo "Killing: `cat $CATALINA_PID`"

kill -9 `cat $CATALINA_PID`

fi

fi

elif [ "$1" = "version" ] ; then

"$_RUNJAVA"\

-classpath "$CATALINA_HOME/server/lib/catalina.jar" \

org.apache.catalina.util.ServerInfo

else

echo "Usage: tomcat5 ( commands ... )"

echo "commands:"

if $os400; then

echo " debug Start Catalina in a debugger (not available on OS400)"

echo " debug -securityDebug Catalina with a security manager (not available on OS400)"

else

echo " debug Start Catalina in a debugger"

echo " debug -securityDebug Catalina with a security manager"

fi

echo " jpda startStart Catalina under JPDA debugger"

echo " runStart Catalina in the current window"

echo " run -securityStart in the current window with security manager"

echo " start Start Catalina in a separate window"

echo " start -securityStart in a separate window with security manager"

echo " stop Stop Catalina"

echo " stop -forceStop Catalina (followed by kill -KILL)"

echo " versionWhat version of tomcat are you running?"

exit 1

fi

amaca at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

Wow, that start/stop script looks totally different from the original one that Tomcat comes with.

And as you pointed out, the block with "### JVM lookup", might perhaps be the culprit.

I'd try and either comment the whole block out, or just after it, add an JAVA_HOME="/path/to/my/jdk"

EXPORT JAVA_HOME

kaderuda at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
I put some lines of debug in, and its definitely choosing the right JVM, so I'm completely bemused.Thanks for the help anyway.
amaca at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
I'm sorry, but this looks to have me beaten, atleast for now :) If I can think if anything, I'll get back to you, otherwise, I wish you the best in tracking down this strange behavior.
kaderuda at 2007-7-12 16:24:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

I had the same problem on a Windows box.

The error is caused by the Jasper compiler in Tomcat 5, by default the Source and Target VM's are 1.3. You must explicitly set them to 1.5

check out:

http://tomcat.apache.org/tomcat-5.0-doc/jasper-howto.html

under Configuration

"compilerSourceVM - What JDK version is the source compatible with. By efault this is 1.3.

compilerTargetVM - What JDK version should the generated files be compatible with. By default this is also 1.3."

Once I explicitly set the init params in %TOMCAT_HOME%/conf/web.xml, it worked.

<servlet>

.....

<init-param>

<param-name>compilerSourceVM</param-name>

<param-value>1.5</param-value>

</init-param>

<init-param>

<param-name>compilerTargetVM</param-name>

<param-value>1.5</param-value>

</init-param>

</servlet>

inryjia at 2007-7-12 16:24:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
Thanks, that's good to know.
amaca at 2007-7-12 16:24:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...