classpath添加在环境变量里不起作用


做李刚老师的例子:
代码:
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java

package lee;

import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.*;

import java.io.PrintWriter;
import java.io.IOException;
import java.sql.*;

public class LoginServlet extends HttpServlet {
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
        String errMsg = "";
        RequestDispatcher rd;//RequestDispatcher is imported
        String username = request.getParameter("username");
        String pass = request.getParameter("pass");
        try {
            DbDao dd = DbDao.instance("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/cara", "root", "stimpy");
            ResultSet rs = dd.query("select pass from user_table where name = '"+ username +"'");// ResultSet?
            if (rs.next()) {
                if (rs.getString("pass").equals(pass)) {
                    HttpSession session = request.getSession(true);
                    session.setAttribute("name", username);
                    rd = request.getRequestDispatcher("/welcome.jsp");//?
                    rd.forward(request, response);
                } else {
                    errMsg += "your username and password are not fit";
                }
            } else {
                errMsg += "your username does not exist, please register first";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if (errMsg != null || !errMsg.equals("")) {
            rd = request.getRequestDispatch("/login.jsp");
            request.setAttribute("err", errMsg);
            rd.forward(request, response);
        }
    }
}


编译通过:
C:\Users\hp2>javac -cp "D:\myWorkspace\test\WEB-INF\src";"E:\software\apache-tom
cat-6.0.26\lib\servlet-api.jar";"E:\software\apache-tomcat-6.0.26\lib\jsp-api.ja
r" "D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java"

如果我将这两个classpath添加到环境变量中,一下是我当前的环境变量:
.;E:\software\apache-tomcat-6.0.26\lib\servlet-api.jar;E:\software\apache-tomcat-6.0.26\lib\jsp-api.jar;%JAVA_HOME%/lib/tools.jar;%jJAVA_HOME%/bin

编译:
C:\Users\hp2>javac -cp "E:\software\apache-tomcat-6.0.26\lib\servlet-api.jar" "D
:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java"

结果:
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:19: cannot find symbol
symbol  : class DbDao
location: class lee.LoginServlet
                        DbDao dd = DbDao.instance("com.mysql.jdbc.Driver", "jdbc
:mysql://localhost:3306/cara", "root", "stimpy");
                        ^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:19: cannot find symbol
symbol  : variable DbDao
location: class lee.LoginServlet
                        DbDao dd = DbDao.instance("com.mysql.jdbc.Driver", "jdbc
:mysql://localhost:3306/cara", "root", "stimpy");
                                   ^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:38: cannot find symbol
symbol  : method getRequestDispatch(java.lang.String)
location: interface javax.servlet.http.HttpServletRequest
                        rd = request.getRequestDispatch("/login.jsp");
                                    ^
3 errors

C:\Users\hp2>javac -cp "D:\myWorkspace\test\WEB-INF\src";"E:\software\apache-tom
cat-6.0.26\lib\servlet-api.jar";"E:\software\apache-tomcat-6.0.26\lib\jsp-api.ja
r" "D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java"
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:38: cannot find symbol
symbol  : method getRequestDispatch(java.lang.String)
location: interface javax.servlet.http.HttpServletRequest
                        rd = request.getRequestDispatch("/login.jsp");
                                    ^
1 error

C:\Users\hp2>javac -cp "D:\myWorkspace\test\WEB-INF\src";"E:\software\apache-tom
cat-6.0.26\lib\servlet-api.jar";"E:\software\apache-tomcat-6.0.26\lib\jsp-api.ja
r" "D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java"
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:38: cannot find symbol
symbol  : method getRequestDispatch(java.lang.String)
location: interface javax.servlet.http.HttpServletRequest
                        rd = request.getRequestDispatch("/login.jsp");
                                    ^
1 error

C:\Users\hp2>javac -cp "D:\myWorkspace\test\WEB-INF\src";"E:\software\apache-tom
cat-6.0.26\lib\servlet-api.jar";"E:\software\apache-tomcat-6.0.26\lib\jsp-api.ja
r" "D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java"
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:38: cannot find symbol
symbol  : method getRequestDispatch(java.lang.String)
location: interface javax.servlet.ServletContext
                        rd = getServletContext().getRequestDispatch("/login.jsp"
);
                                                ^
1 error

C:\Users\hp2>javac -cp "D:\myWorkspace\test\WEB-INF\src";"E:\software\apache-tom
cat-6.0.26\lib\servlet-api.jar";"E:\software\apache-tomcat-6.0.26\lib\jsp-api.ja
r" "D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java"
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:38: cannot find symbol
symbol  : method getRequestDispatch(java.lang.String)
location: interface javax.servlet.ServletContext
                        rd = getServletContext().getRequestDispatch("/login.jsp"
);
                                                ^
1 error

C:\Users\hp2>javac -cp "D:\myWorkspace\test\WEB-INF\src";"E:\software\apache-tom
cat-6.0.26\lib\servlet-api.jar";"E:\software\apache-tomcat-6.0.26\lib\jsp-api.ja
r" "D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java"

C:\Users\hp2>javac -cp "D:\myWorkspace\test\WEB-INF\src" "D:\myWorkspace\test\WE
B-INF\src\lee\LoginServlet.java"
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:3: package javax.servlet d
oes not exist
import javax.servlet.ServletException;
                    ^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:4: package javax.servlet d
oes not exist
import javax.servlet.ServletContext;
                    ^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:5: package javax.servlet d
oes not exist
import javax.servlet.RequestDispatcher;
                    ^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:6: package javax.servlet.h
ttp does not exist
import javax.servlet.http.*;
^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:12: cannot find symbol
symbol: class HttpServlet
public class LoginServlet extends HttpServlet {
                                  ^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:13: cannot find symbol
symbol  : class HttpServletRequest
location: class lee.LoginServlet
        public void service(HttpServletRequest request, HttpServletResponse resp
onse) throws ServletException, java.io.IOException {
                            ^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:13: cannot find symbol
symbol  : class HttpServletResponse
location: class lee.LoginServlet
        public void service(HttpServletRequest request, HttpServletResponse resp
onse) throws ServletException, java.io.IOException {
                                                        ^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:13: cannot find symbol
symbol  : class ServletException
location: class lee.LoginServlet
        public void service(HttpServletRequest request, HttpServletResponse resp
onse) throws ServletException, java.io.IOException {

             ^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:15: cannot find symbol
symbol  : class RequestDispatcher
location: class lee.LoginServlet
                RequestDispatcher rd;//RequestDispatcher is imported
                ^
D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java:23: cannot find symbol
symbol  : class HttpSession
location: class lee.LoginServlet
                                        HttpSession session = request.getSession
(true);
                                        ^
10 errors

找不到很多东西,看来是环境变量设置的问题了,麻烦大家指点一下了!!

11 个解决方案

#1


getRequestDispatch
全没加er

#2


你javahome变量设没有?

#3


设了。请忽略dispather的错误,我已经修正了。
编译时如果是这样:
C:\Users\hp2>javac -cp "D:\myWorkspace\test\WEB-INF\src";"E:\software\apache-tom
cat-6.0.26\lib\servlet-api.jar";"E:\software\apache-tomcat-6.0.26\lib\jsp-api.ja
r" "D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java"

就能编译通过。

如果把servlet-api.jar和jsp-api.jar添加到环境变量里然后这样:
C:\Users\hp2>javac -cp "E:\software\apache-tomcat-6.0.26\lib\servlet-api.jar" "D
:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java"

就不通过了,是很多东西找不到。

#4


引用 2 楼 hchjjun 的回复:
你javahome变量设没有?

设了,JAVA_HOME现在是C:\Program Files (x86)\Java\jdk1.6.0_12

#5


该回复于2010-09-07 09:54:16被版主删除

#6


添加到环境变量之后,为什么还要servlet-api.jar?
C:\Users\hp2>javac -cp "D:\myWorkspace\test\WEB-INF\src" "D:\myWorkspace\test\WEB-INF\src\lee\LoginServlet.java"这样呢?

#7


环境变量直接设置成E:\software\apache-tomcat-6.0.26\lib\这样就行了

#8


classpath的那个

#9


有一点需要确认,那就是,你设置 CLASSPATH 变量之后,你的 cmd.exe 重启过了吗?

环境变量在一个进程启动之后是不会再改变了,只有重开才起作用。

#10


我现在环境变量
classpath是E:\software\apache-tomcat-6.0.26\lib\
重启了cmd窗口
依然出现上述错误。

#11


现在
classpath:E:\software\apache-tomcat-6.0.26\lib\*;这样就是指该目录下所有的jar包,可以解决。
智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告