java.lang.ClassNotFoundException 在动态 Web 应用中 JSP

java.lang.ClassNotFoundException in Dynamic Web app JSP

我正在尝试从 Java Class 中的 SQLLite 数据库检索数据。 根据此处的教程,这是使用 Beans 和表单处理的 Web 应用程序的一部分: http://www.jsptut.com/forms.jsp

我收到 java.lang.ClassNotFoundException 的持续错误:org.sqlite.JDBC

我已将 sqlite-jdbc-3.8.7.jar 添加到 java 类路径

这是我的代码:

   public int startConnection() {
         Connection c = null;
            try {
              Class.forName("org.sqlite.JDBC");
              c = DriverManager.getConnection("jdbc:sqlite:test.db");
            } catch ( Exception e ) {
              System.err.println( e.getClass().getName() + ": " + e.getMessage() );
              System.exit(0);
              return CONNECTION_FAILURE;
            }
            System.out.println("Opened database successfully");
            return CONNECTION_SUCCESSFUL;
    }

这是控制台输出:

Jan 03, 2015 2:42:15 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:webapp01' did not find a matching property.
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.57
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Nov 3 2014 08:39:16 UTC
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.57.0
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Mac OS X
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.10
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86_64
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JAVA_HOME:             /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_25-b17
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /Users/hadi/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /Users/hadi/Documents/apache-tomcat-7.0.57
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/hadi/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/hadi/Documents/apache-tomcat-7.0.57
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/hadi/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Users/hadi/Documents/apache-tomcat-7.0.57/endorsed
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Jan 03, 2015 2:42:15 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/hadi/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Jan 03, 2015 2:42:15 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jan 03, 2015 2:42:15 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jan 03, 2015 2:42:15 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1296 ms
Jan 03, 2015 2:42:15 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 03, 2015 2:42:15 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.57
Jan 03, 2015 2:42:16 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jan 03, 2015 2:42:16 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jan 03, 2015 2:42:16 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 913 ms
java.lang.ClassNotFoundException: org.sqlite.JDBC
Jan 03, 2015 2:42:32 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-bio-8080"]

请任何人解决这个问题......谢谢....

sqlite-jdbc-3.8.7.jar 添加到 tomcat class 路径,方法是将其移动到 $CATALINA_HOME/lib

请参阅 Apache Tomcat 7 - Class Loader Howto 以深入了解 Tomcat class 加载程序的工作原理。它的一部分是

System — This class loader is normally initialized from the contents of the CLASSPATH environment variable. All such classes are visible to both Tomcat internal classes, and to web applications. However, the standard Tomcat startup scripts ($CATALINA_HOME/bin/catalina.sh or %CATALINA_HOME%\bin\catalina.bat) totally ignore the contents of the CLASSPATH environment variable itself, and instead build the System class loader from ...