java won't compile with RequestDispatcher, cla

2019-08-30 07:47发布

问题:

I'm using ubuntu and I've been trying to compile based on my research and my previous posting regarding to compilation issue. This time I suspect CLASSPATH but cannot figure out what is wrong really.

(The java file I want to complie)

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

This is the servlet class that extends HttpServlet thus using RequestDispatcher class.

I have servlet-api.jar in tomcat directory:

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

So I added the path to CLASSPATH then my CLASSPATH looks like:

CLASSPATH=/usr/bin;.;/home/makun/tomcat/apache-tomcat-6.0.26/webapps/head_first/src;/usr/share/java/lib;/home/makun/Desktop/java_run;/home/makun/tomcat/apache-tomcat-6.0.26/webapps/head_first/src;/usr/share/java/lib;/home/makun/tomcat/apache-tomcat-6.0.26/lib;/home/makun/tomcat/apache-tomcat-6.0.26/lib/servlet-api.jar

Now I try to complie RegisterServlet.java from my current directory which is /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

Then evil errors show up...

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

Can anyone help me point out why my complier cannot locate the RequestDispatcher?

EDIT

As per suggestion, I've removed semi-colon and change to colon in my CLASSPATH and also simply run javac -verbose RegisterServlet.java and it complied successfully.

回答1:

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

You're overriding the CLASSPATH env variable here. You also seem to seperate the classpath entries with a ; , on *nix you might need a : instead. If your CLASSPATH env variable is exported, just do

 javac -verbose RegisterServlet.java

or

 javac -cp $CLASSPATH -verbose RegisterServlet.java