Servlet编译错误
我使用 Apache Tomcat 6 开发了简单的 servlet。
首先,我编写了简单的 Hello World 打印 servlet。然后为servlet-api.jar
设置CLASSPATH并编译并复制webapps/login/WEB-INF/classes/test/HelloServlet.class
。效果很好。
在我在 servlet 中编写简单的 JDBC 连接之后。我下载了MySQL J-Connector并像这样设置CLASSPATH:
C:\Program Files\apache-tomcat-6.0.32\lib\servlet-api.jar;C:\Program Files\apache-tomcat-6.0.32\lib\mysql-connector-java-5.1.16-bin.jar
然后尝试编译;然后它显示以下消息:
"Unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown"
我应该如何解决这个问题?
I developed simple servlet using Apache Tomcat 6.
Firstly I write simple Hello World print servlet. Then set CLASSPATH for servlet-api.jar
and compile and copy webapps/login/WEB-INF/classes/test/HelloServlet.class
. That's working fine.
After I write simple JDBC connection in the servlet. I downloaded MySQL J-Connector and set CLASSPATH like this:
C:\Program Files\apache-tomcat-6.0.32\lib\servlet-api.jar;C:\Program Files\apache-tomcat-6.0.32\lib\mysql-connector-java-5.1.16-bin.jar
then try to compile; it then shows the following message:
"Unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown"
How should I solve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的
Class.forName()
放在 try catch 块下类路径应以 (.;) 结尾,检查一次。Keep your
Class.forName()
under try catch block & classpath should ends with (.;) check it once.java.lang.ClassNotFoundException
是一个受检查的异常。这意味着您需要处理它,方法是将可能引发此异常的调用放入try { ... } catch (ClassNotFoundException e) { ... }
块中,或者添加在方法声明中添加一个throws
子句,在该方法中调用可能引发此异常的方法。在捕获或指定要求中了解有关处理已检查异常的更多信息在 Oracle 的 Java 教程中。
java.lang.ClassNotFoundException
is a checked exception. That means that you are required to deal with it, either by putting the call that may throw this exception inside atry { ... } catch (ClassNotFoundException e) { ... }
block or by adding athrows
clause to the method declaration of the method in which you make the call to the method that may throw this exception.Read more about dealing with checked exceptions in The Catch or Specify Requirement in Oracle's Java Tutorials.