wood burning stoves 2.0*
The moose likes JSP and the fly likes Tomcat is not finding my class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Tomcat is not finding my class" Watch "Tomcat is not finding my class" New topic
Author

Tomcat is not finding my class

Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
Hi all, first post. I'm trying to get a servlet running, but it keeps 404ing on me. I checked the FAQ, and other than the use of a package (which I deliberately am not doing yet) everything looks fine. This is on Kubuntu 10.10 with Tomcat6 and Sun java.

First of all, let's make sure that everything is right:


The Page.class file compiled with no problems, and I did restart tomcat after putting everything in it's place. However, when I try the address http://127.0.0.1:8080/test/foo I get a 404 error. I know that this is the right server root directory, because I could edit /var/lib/tomcat6/webapps/ROOT/index.html in VI and see my edits on http://127.0.0.1:8080. So, what appears to be wrong?

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Dotan Cohen wrote:and other than the use of a package (which I deliberately am not doing yet)


This is like saying "And other than putting gas in the tank (which I am deliberately no doing yet)" and wondering why the car won't start.

Put the servlet, and any other classes, in a package.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
All right, Bear, I thought that was unnecessary at this stage but I'm willing to learn! Thanks.

Putting the code in a package didn't help, though. Here is everything that I think one would need to know:


Did I write the web.xml file wrong? This is what I suspect.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Why is test under ROOT?

And foo.html as a mapping is folly. You're going to screw up the caching because everything assumes that anything ending with .html is a static resource.
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
Bear Bibeault wrote:Why is test under ROOT?


Because ROOT is the tomcat webroot. Was that a bad assumption on my part? Moving test/ up a directory (to be a sibling of ROOT) results in the same situation, so where should I put it?


And foo.html as a mapping is folly. You're going to screw up the caching because everything assumes that anything ending with .html is a static resource.


Yes, of course. This is just until I get "Hello, world" out the door. Though to be honest, even on production dynamic sites I do prefer the .html ending and the proper headers to prevent caching. The nice thing about that is that I can use different technologies under the hood (Python, PHP). But that is a whole other discussion, to be sure!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Dotan Cohen wrote:Because ROOT is the tomcat webroot.

It is not. It is the root web application. You are trying to put one web app inside another.

For automatic deployment, web apps should have their context root (folder with WEB-INF) in the webapps folder.

Though to be honest, even on production dynamic sites I do prefer the .html ending and the proper headers to prevent caching.

Maximum folly. All you will do is to create non-deterministic behavior. If you want to prevent caching, just do so with the proper HTTP headers.

The nice thing about that is that I can use different technologies under the hood (Python, PHP). But that is a whole other discussion, to be sure!

There is no nice thing about it. It will lead to eventual disaster. I have the scars to prove it. Use mappings without any associated file type.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Why do you keep showing the source folders? They are of no runtime interest. The context root and below are all that matter at run time.

Have you used the Tomcat manager to verify that your web app is even loading?
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
Bear Bibeault wrote:
Dotan Cohen wrote:Because ROOT is the tomcat webroot.

It is not. It is the root web application. You are trying to put one web app inside another.

For automatic deployment, web apps should have their context root (folder with WEB-INF) in the webapps folder.


Thanks, I did not know that. Well, it is now in webapps (see previous post) but it still won't find the class. So I must have another error somewhere.

Maximum folly. All you will do is to create non-deterministic behavior. If you want to prevent caching, just do so with the proper HTTP headers.

I do just that! I don't rely on the filename to prevent caching!


There is no nice thing about it. It will lead to eventual disaster. I have the scars to prove it. Use mappings without any associated file type.

I know that you are right. So far I've been careful with .htaccess to determine which technology should parse which page. I know that is an accident waiting to happen. It's just hobby sites like my homepage, though, nothing critical. When I finish my studies I certainly _won't_ play around like that on an employer or customer's site!

I really do appreciate your patience in helping me. If there is a fine manual that I should be reading, I'd love to know the URL. I am sharing a copy of the JSP and Servlets Head First book with another student, but it's now at his place and I won't get it back until Wednesday. In the meantime, I'd really like to get tomcat parsing and serving my pages.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Read the Tomcat documentation. Get the manager app running.
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
Bear Bibeault wrote:Read the Tomcat documentation. Get the manager app running.


Thanks, Bear. I found this fine manual:
http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html

I configured a manager in /etc/tomcat6/tomcat-users.xml and logged in with HTTP credentials. I then tried these two commands to deploy the servlet:



I also tried with the http://127.0.0.1:8080/manager/html interface, but with all that I've played around with the values I get the same failure error as above. However, I seem to now be stuck with this entry that will not start and will not undeploy:
Path: /test
Display Name:
Running: false
Sessions: 0

Clicking "start" gives this error:
FAIL - Application at context path /test could not be started

And clicking "undeploy" gives this message:
OK - Undeployed application at context path /test
However, the application still appears in the list and I still cannot start it.

I'm at a loss. Where should I go from here?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Time to look in the logs. Something is keeping your app from deploying.
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
Bear Bibeault wrote:Time to look in the logs. Something is keeping your app from deploying.


Thanks, I found this in the catalina logs:


The line/column in question is the end of the </servlet-mapping> line. Googling I don't see exactly what I should put in the <url-pattern> tag. In fact, it looks completely optional from what I gather. I called Natalia (the girl with the Head First book) and she insists that on the example page in the first chaper there is no url-pattern tag, in fact, she insists (and I remember) that the example wasn't in a package either!

What do you think, Bear? The fine manual is a bit terse for me to understand what is necessary, and I found no tutorials for this config file. I found people having trouble with asterisks and such, but nothing relevant for my issue.

The catalina log is here if you need:
http://pastebin.com/CsZE3dbY

And here is the localhost log:
http://pastebin.com/Uk6CxHX6

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
What are the contents of web.xml now?

Also, none of the later "tree" outputs shows any class files inside of WEB-INF/classes.


Ping & DNS - updated with new look and Ping home screen widget
Dotan Cohen
Ranch Hand

Joined: Feb 09, 2011
Posts: 40
Ulf Dittmer wrote:What are the contents of web.xml now?


I changed it to this:


Now, I'm getting a 404 error, but it is complaining about not finding the class:


Also, none of the later "tree" outputs shows any class files inside of WEB-INF/classes.


You are right! That was the problem, along with the web.xml issue (and not being in webapps earlier). I'm such a noob!



Ulf and Bear, thank you very, very much for your help and your patience. I certainly learned a lot from this issue, not the least of which is to familiarise myself with the Tomcat docs and log files. Have a terrific week!
 
Consider Paul's rocket mass heater.
 
subject: Tomcat is not finding my class
 
Similar Threads
Java Servlets and JSP pages
Problems with the Example from HF Servlets & JSP in Chapter 3
applet and servlet not communicating... any ideas?
runtime ERROR
I am not able to Run Head First servlet example