How to fix the Tomcat 7 private instance and Eclipse integration error in Ubuntu

The first step as part of my journey in learning Spring MVC was to create a private instance of Tomcat 7. The procedure I had followed is documented as part of my earlier post: How to setup multiple instances of Tomcat 7 in Ubuntu.

The next step was to setup Eclipse to recognize the Tomcat 7 private instance.

A new server instance can be registered in Eclipse using Window -> preferences -> Server -> Runtime Environments menu.

But, when I try add the private Tomcat 7 instance, Eclipse reports that it could not identify the version of the Tomcat and refuses to register the instance.

Following the suggestions by David Edwards at askubuntu.com for a problem not directly linked to my problem, solved the Eclipse-Tomcat7-private-instance-integration issue.

The solution suggested by David Edwards was to create a link for the lib and bootstrap.jar library in the private instance folder.

Due to missing fix for the bug-297675 in the tomcat7-user package, the tomcat7-instance-create script fails to setup everything as required by Eclipse when executed.

The part of the solution that I followed can be divided into two steps.

Step 1: Create a link to the lib folder and the bootstrap.jar file located at /usr/share/tomcat7 in the tomcat7 private instance directory.

ln -s /usr/share/tomcat7/lib ~/Workspace/github/springapp/tomcat//lib
ln -s /usr/share/tomcat7/bin/bootstrap.jar ~/Workspace/github/springapp/tomcat/bin/bootstrap.jar

In addition, I also had to create a link to /usr/share/tomcat7/bin/tomcat-juli.jar.

ln -s /usr/share/tomcat7/bin/tomcat-juli.jar ~/Workspace/github/springapp/tomcat/bin/tomcat-juli.jar

Note: ~/Workspace/github/springapp/tomcat is my Tomcat 7 private instance folder created using tomcat7-intance-create script.

Step 2: Either download the catalina.policy file from code repository or from within the general Tomcat 7 instance configuration folder. For instance:

# If you opt for downloading the file - replace 'precise' with the right name of your Ubuntu release.
curl http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/precise/tomcat7/precise/download/head:/catalina.policy-20110521092527-dk8tcyjy21h8eu9h-1652/catalina.policy > ~/Workspace/github/springapp/tomcat/conf/catalina.policy

Note: Make sure that you are downloading the latest version of http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/precise/tomcat7/precise/view/head:/conf/catalina.policy file.

Eclipse should be able to recognize the version of Tomcat without issues.

Meanwhile, shutdown and restart the private instance using the commands:

settipalli@settipalli-ubuntu:~/Workspace/github/springapp/tomcat$ ./bin/shutdown.sh

# Followed by:
settipalli@settipalli-ubuntu:~/Workspace/github/springapp/tomcat$ ./bin/startup.sh

References:
1.How to setup multiple instances of Tomcat 7 in Ubuntu
2.Unknown version of Tomcat was specified with tomcat-7.0.42
3.Tomcat and Eclipse Integration Error : “Unknown version of Tomcat was specified.”
4.Cannot start tomcat after installing a private instance
5.Tomcat 7 and Eclipse integration getting error
6.Configure TOMCAT in Eclipse
7.Eclipse Community Forums – Can’t add a new Tomcat 7 Server
8.Eclipse 4.2 (Juno) ‘Cannot create a server using the selected type’ in Tomcat 7
9.Eclipse can’t find catalina.policy and bootstrap.jar where it expects them
10.Man page of tomcat7-instance-create

How to setup multiple instances of Tomcat 7 in Ubuntu

I was trying to learn Spring MVC and was interested to setup a dedicated instance of Apache Tomcat 7 for the pet project rather than using the single installed instance. I used a Ubuntu 12.04 LTS desktop edition while performing the below steps.

Using a dedicated instance rather than a general instance would allow me load custom libraies and not tamper with the settings and libraries utilized by the general instance of Tomcat 7.

Step 1: Install the tomcat7-user package.

sudo apt-get install tomcat7-user

Step 2: Setup a parent folder where you would like to store the instance specific configuration and libraries

mkdir $HOME/Workspace/github/springapp

I have specified a path specific to my project folder. You can choose any folder of your choice.

Step 3: Use the tomcat7-instance-create command to create and configure the Tomcat 7 instance folder at $HOME/Workspace/github/springapp/tomcat.

tomcat7-instance-create $HOME/Workspace/github/springapp/tomcat

# SYNOPSIS FOR REFERENCE
#       tomcat7-instance-create [OPTIONS] DIRECTORYNAME

The default port used by the instance would be 8080. If you would like have the new instance listen to a different port, use the -p switch. This is useful when you like to have both the general and the dedicated instance of Tomcat running simulataneously.

Similary, the -c switch allows us to specify a control port which can be used to send ‘Magic Words’ that cause Tomcat to trigger specific actions. For instance, the default magic word to gracefully shutdown Tomcat is SHUTDOWN and the default control port is 8005.

As an example, to have Tomcat listen on port 8085 with control port being 8010 and the Magic Word to shutdown Tomcat being TAKEYOURLOADOFF:

tomcat7-instance-create -p 8085 -c 8010 -w TAKEYOURLOADOFF $HOME/Workspace/github/springapp/tomcat

The output would be similar to:

You are about to create a Tomcat instance in directory '$HOME/Workspace/github/springapp/tomcat'
* New Tomcat instance created in $HOME/Workspace/github/springapp/tomcat
* You might want to edit default configuration in $HOME/Workspace/github/springapp/tomcat/conf
* Run $HOME/Workspace/github/springapp/tomcat/bin/startup.sh to start your Tomcat instance

Note: The variable $HOME would be replaced with the absolute path of the directory in your output.

To learn more about the options available for the tomcat7-instance-create command, please refer the man page:

[man tomcat7-instance-create][3]

Step 4: Start the new instance of Tomcat 7.

settipalli@settipalli-ubuntu:~/Workspace/github/springapp$ tomcat/bin/startup.sh
Using CATALINA_BASE:   $HOME/Workspace/github/springapp/tomcat
Using CATALINA_HOME:   /usr/share/tomcat7
Using CATALINA_TMPDIR: $HOME/Workspace/github/springapp/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar
Tomcat started

Step 5: To verify if the new instance of Tomcat has loaded successfully, navigate to ‘localhost:8085‘ in your web-browser. You would be greeted with an empty page. This is normal.

To confirm if Tomcat has started and is listening on the specific ports, execute the below command.

settipalli@settipalli-ubuntu:~/Workspace/github/springapp$ netstat -punta | grep java
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp6       0      0 :::8085                 :::*                    LISTEN      4862/java
tcp6       0      0 127.0.0.1:8010          :::*                    LISTEN      4862/java

To reconfirm if everthing is fine, look into the logs at: $HOME/Workspace/github/springapp/tomcat/logs/localhost_access_log..txt. The content should be similar to:

127.0.0.1 - - [30/Jan/2015:04:03:55 +0530] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [30/Jan/2015:04:03:56 +0530] "GET /favicon.ico HTTP/1.1" 404 -

Since we have not configured any web-apps to be servered by Tomcat, 404 response is valid.

Similary, review $HOME/Workspace/github/springapp/tomcat/logs/catalina.out for Tomcat startup messages.

To shutdown the instance, use the below command:

settipalli@settipalli-ubuntu:~/Workspace/github/springapp$ tomcat/bin/shutdown.sh

# Sample Output of the command
Using CATALINA_BASE:   /home/settipalli/Workspace/github/springapp/tomcat
Using CATALINA_HOME:   /usr/share/tomcat7
Using CATALINA_TMPDIR: /home/settipalli/Workspace/github/springapp/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar
Jan 30, 2015 4:11:31 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/common/classes], exists: [false], isDirectory: [false], canRead: [false]
Jan 30, 2015 4:11:31 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/common], exists: [false], isDirectory: [false], canRead: [false]
Jan 30, 2015 4:11:31 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/server/classes], exists: [false], isDirectory: [false], canRead: [false]
Jan 30, 2015 4:11:31 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/server], exists: [false], isDirectory: [false], canRead: [false]
Jan 30, 2015 4:11:31 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/shared/classes], exists: [false], isDirectory: [false], canRead: [false]
Jan 30, 2015 4:11:31 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/shared], exists: [false], isDirectory: [false], canRead: [false]
Tomcat stopped

References:
1. Add another instance of Tomcat, on Ubuntu
2. How to install Multiple instances of tomcat on a single ubuntu server
3. tomcat7-instance-create – Gists – GitHub
4. Ubuntu 12.04.5 LTS (Precise Pangolin)
5. Man page of tomcat7-instance-create