aspose file tools*
The moose likes Tomcat and the fly likes  Tomcat not loading its jars to classpath at run time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark " Tomcat not loading its jars to classpath at run time" Watch " Tomcat not loading its jars to classpath at run time" New topic
Author

Tomcat not loading its jars to classpath at run time

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear All,
I'm using Tomcat 5.5.27 version & this is my configuration of environment variables.


I've read that

Tomcat automatically appends the location of its jar files to your existing CLASSPATH.


Then I created a simple webapplication with the following directory structure:

My java source is in src/WebTest.java



when I compile it :


1)It clearly meant that Tomcat has not loaded its jar servlet-api.jar into the classpath & why is it happening & what can be done to rectify this

2) Since I've ant I want to use Ant to separate source from the generated class files.
How to use that.
If i have the package structure com.common.java;
then the class files should be in webinf/classes/com/common/java/WebTest.class
How this can be achieved using Ant.
Remember ant is in my path.

BestRegards
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

where you placed your servlet-api.jar . better to place in tomcat/common/lib
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
The server was started using startup.bat
This is the document trace which i got when I run the server
I ran the server by

Server trace:

Now when i compile, by appending the jar manually to the classpath it's working.

1)Why we should manually append the jar to the classpath doesn't Tomcat does it of its own

2) How to use Ant here to separate java from class files.
Regards.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear seetharaman venkatasamy,
My servlet-api.jar is only in CATALINA_HOME\common\lib.
Where CATALINA_HOME is pointing to E:\apache-tomcat-5.5.27
Regards.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

1)It clearly meant that Tomcat has not loaded its jar servlet-api.jar into the classpath & why is it happening


Because here you are compiling the source files which has nothing to do with the tomcat (It's JAVA compiler you are using ). By the way it's really a bad practice to put the source files & compiling from inside the Application server.

Keep your development environment out of application server. When you want to deploy you may create a WAR or move the necessary files to the application server.

If i have the package structure com.common.java;
then the class files should be in webinf/classes/com/common/java/WebTest.class
How this can be achieved using Ant.


You have to use Ant's javac Task with srcdir and destdir attributes. Have a look at the manual for complete information.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
After the class gets compiled , i enhanced the web.xml .
This is my web.xml

My class file is in classes folder.
1)Then I created a war file using

2) Copied these war file in webapps folder

3) Then I shutdown the server & restarted the server.

4) When I try to run


It says an error

HTTP Status 404 - /FirstApplication/

--------------------------------------------------------------------------------

type Status report

message /FirstApplication/

description The requested resource (/FirstApplication/) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.27


How this can be fixed.
Is my web.xml proper.
I'm using tomcat version 5.5.27
Regards.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
What about http://localhost:8080/FirstApplication/start-web-test since that's the only resource you have mapped?

Does the Tomcat Manager (usually at http://localhost:8080/manager/html/list) list the application as running?
[ December 23, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338

What about http://localhost:8080/FirstApplication/start-web-test since that's the only resource you have mapped?

Correct ! Now when I go over the above link it's working & I get the output

A Simple Web Application
Here's some text...
...and a bit more text


But,

Does the Tomcat Manager (usually at http://localhost:8080/manager/html/list) list the application as running?

No.
The error it's showing is:

HTTP Status 403 - Access to the requested resource has been denied

--------------------------------------------------------------------------------

type Status report

message Access to the requested resource has been denied

description Access to the specified resource (Access to the requested resource has been denied) has been forbidden.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.27


Thanks Ulf Dittmer for your valuable input.

For vijitha,

By the way it's really a bad practice to put the source files & compiling from inside the Application server.

I'm very eager to know why is it so? Because inside the application, i'm separating the source & class files right.


Because here you are compiling the source files which has nothing to do with the tomcat (It's JAVA compiler you are using )

But i'm using some dependencies ( some classes of servlet-api.jar) in my source file(WebTest.java) which directly access the tomcat dependencies only right.

You have to use Ant's javac Task with srcdir and destdir attributes. Have a look at the manual for complete information.

Certainly & thanks for the links.
Regards.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Doesn't Tomcat load all of its jars inside common\lib to classpath when we start the server .
I have JAVA_HOME & also CATALINA_HOME defined properly, which is echoed when the server is started up.(Please see my second post)
Regards.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
Doesn't Tomcat load all of its jars inside common\lib to classpath when we start the server.

Yes it does, but as was said above, Tomcat has nothing to do with the Java compiler. And compiling servlets has no dependency on Tomcat - rather it depends on the jar file that contains the Servlet API. Tomcat just happens to include this jar file, that's all.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Understood !
When we are compiling, we are compiling with the java compiler javac, which as of such doesn't know about servlet-api.jar or any other jars, which has to be directed using javac -classpath.
Then what is the use of Tomcat loading all the jars at the time of starting the server?
Regards.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
How could Tomcat function if it didn't have the servlet API and all those other jar files?
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
For what function does Tomcat is loading these jars like servlet-api.jar & other jars in the classpath.
Regards.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Any replies?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
Once again:
Originally posted by Ulf Dittmer:
How could Tomcat function if it didn't have the servlet API and all those other jar files available?


If you're curious what's inside of these jars, you can use "java -tf foobar.jar" to see all the classes. And if you're really curious how it all works, well, it's open source, so the source code is available for study.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tomcat not loading its jars to classpath at run time