Hello friends, my servlet class is like this.. my diretory structure is C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\testJsp\WEB-INF\classes where i have the newBean class
newBean.java:2: package javax.servlet does not exist import javax.servlet.*; ^ newBean.java:3: package javax.servlet does not exist import javax.servlet.http; ^ newBean.java:5: cannot find symbol symbol: class HttpServlet public class newBean extends HttpServlet { ^ newBean.java:7: cannot find symbol symbol : class HttpServletRequest location: class newBean public void doPost(HttpServletRequest request,HttpServletResponse response) ^ newBean.java:7: cannot find symbol symbol : class HttpServletResponse location: class newBean public void doPost(HttpServletRequest request,HttpServletResponse response) ^ newBean.java:9: cannot find symbol symbol : class ServletException location: class newBean throws ServletException,IOException { ^ newBean.java:9: cannot find symbol symbol : class IOException location: class newBean throws ServletException,IOException { ^ newBean.java:11: cannot access foo.Person bad class file: .\foo\Person.java file does not contain class foo.Person Please remove or make sure it appears in the correct subdirectory of the classpa th. Person p=new Person(); ^ 8 errors
Because this is more of a javac/classpath issue than something specific to the servlet spec, I'm going to move it (back) to our Java In General (Beginner) forum where compilation and classpath issues are best discussed. [ November 26, 2007: Message edited by: Ben Souther ]
Dear Bert Bates, I am a fan of yours.I am reading HeadFirst servlets and JSP. I have and always had problems with classpath.this is a nagging problem. Some days ago i compiled servlets,and ran the programs successfully with Tomcat,as written in your book,but this same problem has occured again.I dont know what goes wrong. Hence i am asking for help. Regards, Ayanik
You're too quick for me. You responded before I had a chance to move this thread.
I think your last post re-enforces my point. Before tackling servlet development, it is important that you are comfortable with the fundamentals of Java itself. Specifically, you should understand javac, packages, and classpaths. In my opinion, these things are best learned on plain old java objects with a text editor and the command line compiler 'javac'.
1.) The javax.servlet... classes (dependencies) are not on your classpath. How are you passing your classpath to javac?
2.) It looks like you may have a java file with a different name or package than what is indicated in its source
newBean.java:11: cannot access foo.Person bad class file: .\foo\Person.java file does not contain class foo.Person Please remove or make sure it appears in the correct subdirectory of the classpath
Check that the package statement and the class signature are correct.
Dear Ben, under WEB-INF/classes,there is a package called foo where there is a class called Person.java
the source code is package foo; public class Person { private String name; public String getName() { return name; } public void setName(String name) {
this.name=name;
}}
I tried to import the package in my earlier code.
Also, n my classpath i have the servlet-api.jar,but it is still not compiling. when i give echo %classpath% i have C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar
I corrected the files and here is the error message. newBean.java:1: package foo does not exist import foo.*; ^ newBean.java:8: cannot find symbol symbol : class IOException location: class newBean throws ServletException,IOException { ^ newBean.java:10: cannot find symbol symbol : class Person location: class newBean Person p=new Person(); ^ newBean.java:10: cannot find symbol symbol : class Person location: class newBean Person p=new Person(); ^ 4 errors
Hello friends, the problem is with import statements actually. if for example, i write file BeerSelect.java in com/example/web directory where I import BeerExpert.java from com/example/model directory, the compiler cannot find it.
the java version is java version "1.5.0" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64) Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing) I have never faced such odd problems before.
here are the source codes package com.example.web;
public class BeerSelect extends HttpServlet { public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
public class BeerExpert { public List getBrands(String color) { List brands=new ArrayList(); if (color.equals("amber")) { brands.add("Jack Amber"); brands.add("Red Moose"); }
else { brands.add("Jail Pale Ale"); brands.add("Gout Stout"); } return brands;
}}
Please help.
Ernest Friedman-Hill
,
author and iconoclast
staff
You need to also have "." (dot), representing the current directory, on your classpath, or the compiler won't be able to find your other files. So the classpath should be at least