java将无法使用RequestDispatcher进行编译,正确设置了类路径

时间:2010-08-26 21:53:37

标签: java servlets compiler-errors

我正在使用ubuntu而且我一直在尝试根据我的研究和之前关于编译问题的帖子进行编译。这次我怀疑CLASSPATH但是无法弄清楚到底出了什么问题。

(我要编译的java文件)

/home/makun/tomcat/apache-tomcat-6.0.26/webapps/head_first
/src/com/masatosan/registerservlet/RegisterServlet.java

这是使用RequestDispatcher类扩展HttpServlet的servlet类。

我在tomcat目录中有servlet-api.jar:

/home/makun/tomcat/apache-tomcat-6.0.26/lib/servlet-api.jar

所以我添加了CLASSPATH的路径,然后我的CLASSPATH看起来像:

CLASSPATH =的/ usr /仓; /家庭/ makun / Tomcat的/ Apache的Tomcat的6.0.26 / web应用/ head_first / SRC;在/ usr /共享/ JAVA / lib中; /家庭/ makun /桌面/ JAVA_RUN ; /home/makun/tomcat/apache-tomcat-6.0.26/webapps/head_first/src;在/ usr /共享/ JAVA / lib中; /home/makun/tomcat/apache-tomcat-6.0.26/lib ; /home/makun/tomcat/apache-tomcat-6.0.26/lib/servlet-api.jar

现在我尝试从当前目录编译RegisterServlet.java,该目录是 /home/makun/tomcat/apache-tomcat-6.0.26/webapps/head_first     / src / com / masatosan / registerservlet

javac -verbose -cp。:/ home / makun / tomcat / apache-tomcat-6.0.26 / lib ./RegisterServlet.java

然后出现了邪恶的错误......

makun /home/makun/tomcat/apache-tomcat-6.0.26/webapps/head_first/src/com/masatosan/registerservlet ->javac -verbose -cp .:/home/makun/tomcat/apache-tomcat-6.0.26/lib ./RegisterServlet.java 
[parsing started ./RegisterServlet.java]
[parsing completed 81ms]
[search path for source files: .,/home/makun/tomcat/apache-tomcat-6.0.26/lib]
[search path for class files: /usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/resources.jar,/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/rt.jar,/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/sunrsasign.jar,/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/jsse.jar,/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/jce.jar,/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/charsets.jar,/usr/lib/jvm/java-6-sun-1.6.0.20/jre/classes,/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/ext/sunpkcs11.jar,/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/ext/sunjce_provider.jar,/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/ext/localedata.jar,/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/ext/dnsns.jar,.,/home/makun/tomcat/apache-tomcat-6.0.26/lib]
[loading java/io/IOException.class(java/io:IOException.class)]
[loading java/util/ArrayList.class(java/util:ArrayList.class)]
[loading java/util/HashMap.class(java/util:HashMap.class)]
[loading java/util/List.class(java/util:List.class)]
[loading java/util/Map.class(java/util:Map.class)]
./RegisterServlet.java:9: package javax.servlet does not exist
import javax.servlet.RequestDispatcher;
                    ^
./RegisterServlet.java:10: package javax.servlet does not exist
import javax.servlet.ServletException;
                    ^
./RegisterServlet.java:11: package javax.servlet.http does not exist
import javax.servlet.http.HttpServlet;
                         ^
./RegisterServlet.java:12: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletRequest;
                         ^
./RegisterServlet.java:13: package javax.servlet.http does not exist
import javax.servlet.http.HttpServletResponse;
                         ^
./RegisterServlet.java:16: cannot find symbol
symbol: class HttpServlet
public class RegisterServlet extends HttpServlet {
                                     ^
[loading java/lang/String.class(java/lang:String.class)]
./RegisterServlet.java:24: cannot find symbol
symbol  : class HttpServletRequest
location: class com.masatosan.registerservlet.RegisterServlet
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                   ^
./RegisterServlet.java:24: cannot find symbol
symbol  : class HttpServletResponse
location: class com.masatosan.registerservlet.RegisterServlet
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                                               ^
./RegisterServlet.java:24: cannot find symbol
symbol  : class ServletException
location: class com.masatosan.registerservlet.RegisterServlet
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                                                                                    ^
./RegisterServlet.java:28: cannot find symbol
symbol  : class HttpServletRequest
location: class com.masatosan.registerservlet.RegisterServlet
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    ^
./RegisterServlet.java:28: cannot find symbol
symbol  : class HttpServletResponse
location: class com.masatosan.registerservlet.RegisterServlet
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                                                ^
./RegisterServlet.java:28: cannot find symbol
symbol  : class ServletException
location: class com.masatosan.registerservlet.RegisterServlet
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                                                                                     ^
[checking com.masatosan.registerservlet.RegisterServlet]
[loading java/util/AbstractMap.class(java/util:AbstractMap.class)]
[loading java/lang/Object.class(java/lang:Object.class)]
[loading java/lang/Exception.class(java/lang:Exception.class)]
[loading java/lang/Throwable.class(java/lang:Throwable.class)]
./RegisterServlet.java:55: cannot find symbol
symbol  : class RequestDispatcher
location: class com.masatosan.registerservlet.RegisterServlet
  RequestDispatcher dispatcher = request.getRequestDispatcher(address);
  ^
[loading java/lang/CharSequence.class(java/lang:CharSequence.class)]
[loading java/io/Serializable.class(java/io:Serializable.class)]
[loading java/lang/Comparable.class(java/lang:Comparable.class)]
[total 828ms]
13 errors

任何人都可以帮我指出为什么我的编译器无法找到RequestDispatcher吗?

修改

根据建议,我已经删除了分号并在我的CLASSPATH中更改为冒号,并且只需运行 javac -verbose RegisterServlet.java 并成功编译。

1 个答案:

答案 0 :(得分:2)

 javac -verbose -cp .:/home/makun/tomcat/apache-tomcat-6.0.26/lib ./RegisterServlet.java

你在这里覆盖了CLASSPATH env变量。您似乎也使用;分隔类路径条目,在* nix上您可能需要:。如果您的CLASSPATH env变量是exported,请执行

 javac -verbose RegisterServlet.java

 javac -cp $CLASSPATH -verbose RegisterServlet.java