Category Archives: Tomcat web server

java.lang.IllegalStateException: Web app root system property already set to different value: ‘webapp.root’ = [/ApplicationPath1] instead of [/ApplicationPath2] – Choose unique values for the ‘webAppRootKey’ context-param in your web.xml files!

Recently I have deployed two J2EE web application in tomcat web server. Both application uses the log4j. Tomcat server is up and running with no issues.

But at regular interval, one of the J2EE web application was going down. I was digging into the logs and google to find the solution. At regular times, it made us to enter into the tomcat manager page to start the application manually which was down.

Then I have identified the following exception message

java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [C:\Program Files\Apache Software Foundation\Tomcat7.0.57\webapps\application1\] instead of [C:\Program Files\Apache Software Foundation\Tomcat7.0.57\webapps\application2\] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:156)
at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:117)
at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:46)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1245)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1895)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Root cause:

Due to the business requirement, we copied the application1’s code base and created the application2’s code base.
I gave unique names wherever necessary in the configuration files. But still I encountered the above error message. After spending long time, I got the below information about the spring framework

“WARNING: Some containers (like Tomcat) do not keep system properties separate per web app. You have to use unique “webAppRootKey” context-params per web app then, to avoid clashes. Other containers like Resin do isolate each web app’s system properties: Here you can use the default key (i.e. no “webAppRootKey” context-param at all) without worrying. ”

It turns out that the webAppRootKey is a Spring context parameter. It exposes the web application’s root directory as a system property. So, it is the value of the webAppRootKey that provides the name for the system property to use. That’s fine if you’ve just got one webapp in Tomcat. Spring will just choose a default value for this (app.root) and off you go. However, as the warning above notes, Tomcat does not keep system properties separate per webApp. This means that when we get to the second application, the same system property (app.root) is being set to a new value (our second application’s root directory, rather than the first!)

The reason this is occurring at all is that Log4JConfigListener is trying to set the webAppRootKey in both cases. This allows log4J to place log and config files in directories relative to the application root directory, rather than in a fixed path.

I decided the easiest thing was just to use a non-default name for the webAppRootKey for each of my applications.

That is, I fixed this by placing the following context-parameter into each of the web application’s web.xml files:

<code><context-param>
<param-name>webAppRootKey</param-name>
<param-value>uniqueAppRootNamee</param-value>
</context-param></code>

This fixed the problem

Advertisements

java.lang.NoClassDefFoundError: Lorg/apache/commons/logging/Log;

Caused by: java.lang.NoClassDefFoundError: Lorg/apache/commons/logging/Log;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Unknown Source)
at java.lang.Class.getDeclaredFields(Unknown Source)
at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106)
at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:270)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:139)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:65)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:403)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:879)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:374)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5378)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 6 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.Log
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
... 20 more

Solution

Adding the commons-logging.jar to the class path will solve the problem.

Download the same from http://commons.apache.org/proper/commons-logging/download_logging.cgi

Happy Learning 🙂

SEVERE: Servlet /Spring3HibernateApp threw load() exception java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

I was working on the Spring MVC example. When I deployed my Spring web application into the Apache Tomcat 7 server, I end up with the following exception


Oct 12, 2014 8:24:01 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /Spring3HibernateApp threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:530)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:512)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5229)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5516)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Solution

ClassNotFoundException should always makes you to think “JAR is missing”. That is, the eclipse could not able to locate the class file ‘DispatcherServlet” here. The ClassNotFoundException clearly indicates that you are missing org.springframework.web.servlet classes.

So make sure the Spring MVC JAR files is available in the classpath. The Spring JARs and all their dependencies should be in your /WEB-INF/lib.

Add all JAR files in the Project build path, which is, Right-click on Project -> Properties -> Java Build Path -> Libraries. Add all external JARS as like either user library or web app libraries. Click on “Add External JARs” to add all the JAR files.

Even after this, if the exception occurs, try the below instructions

1) Project Properties -> Deployment Assembly
2) Add -> Java Build Path Entries
3) You should now see the list of libraries on your build path that you can specify for inclusion into your finished WAR.
4) Select the ones you want and hit Finish.

Happy Learning 🙂

Java Web Start Application fails: connection refused exception when trying to run a jar via Java web start

I wrote my first Java Web Start application. Here as follows,

Step 1: My simple Hello World Swing java program

com.saravanansivaji.swing.HelloWorldSwing.java


package com.saravanansivaji.swing;

import java.awt.EventQueue;
import javax.swing.JFrame;

public class HelloWorldSwing extends JFrame {

public HelloWorldSwing() {

setTitle("Hello World example");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
HelloWorldSwing ex = new HelloWorldSwing();
ex.setVisible(true);
}
});
}
}

Step 2: I packed this as executable JAR file, myswing.jar

Step 3: Created a web application folder “apps” under Tomcat Server web application directory “webapps”.

E:\J2EE\apache-tomcat-6.0.18\webapps\apps

Step 4: Created a JNLP (Java Network Launch Protocol) file in the web folder “apps”, myswing.jnlp,

<?xml version=”1.0″ encoding=”utf-8″?>

<jnlp spec=”0.2 1.0″
codebase=”http://localhost:8081/apps&#8221;
href=”myswing.jnlp”>

<information>
<title>Welcome to Swing World</title>
<vendor>Saravanan Sivaji</vendor>
<homepage href=”index.html”/>
<description>Hello Swing World</description>
<icon href=”saravanansivaji.JPEG”/>
<offline-allowed/>
</information>

<resources>
<j2se version=”1.5+”/>
<jar href=”myswing.jar”/>
</resources>

<application-desc main-class=”com.saravanansivaji.swing.HelloWorldSwing”/>

</jnlp>

Step 5: Create an index page that invokes JNLP file

index.html

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>

<BODY>
<a href=”myswing.jnlp”>Run Swing Master</a>
</BODY>
</HTML>

So finally, my tomcat web folder “apps” contains the following files
1) a JAR file to launch
2) a JNLP file to invoke JAR
3) a index.html file to invoke JNLP file

Now running the tomcat web server and hitting the browser with URL

http://localhost:8080/apps/

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at com.sun.deploy.net.BasicHttpRequest.doRequest(Unknown Source)
at com.sun.deploy.net.BasicHttpRequest.doRequest(Unknown Source)
at com.sun.deploy.net.BasicHttpRequest.doGetRequest(Unknown Source)
at com.sun.javaws.jnl.LaunchDescFactory.buildDescriptor(Unknown Source)
at com.sun.javaws.Launcher.updateFinalLaunchDesc(Unknown Source)
at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source)
at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source)
at com.sun.javaws.Launcher.launch(Unknown Source)
at com.sun.javaws.Main.launchApp(Unknown Source)
at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
at com.sun.javaws.Main$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

The issue occurred, my tomcat web server was running on 8081 port. But I had hit on localhost:8080 port.

Java web start application was launched the HelloWorldSwing component successfully.

Happy ending 🙂

How to change the Tomcat webserver default port

Sometimes there will be a requirement to change the default port number of Tomcat web server over which it runs.

So how to change? Very simple!!

Locate the server.xml file in the tomcat installation directory. It will be available at {Tomcat Installation home directory}\conf folder.

For example,

Here is my tomcat installation directory is E:\J2EE\apache-tomcat-6.0.18. server.xml file can be located at E:\J2EE\apache-tomcat-6.0.18\conf\server.xml

Now, open the server.xml and search for the similar lines,

<Connector port="8081” protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″ />

Here the above lines states, Tomcat server runs on 8081 port.

So if from browser, if we hit as http://localhost:8081/, we can see the Tomcat server’s default page.

For example, the below instruction tells Tomcat server to run on port 8282,

<Connector port="8282” protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″ />

Happy ending!! 🙂