我在ant tests的时候报下面这个错误: 
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver但是我用ant编译之后起动tomcat后什么都好用。为什么测试的时候出错啊?
Class.forName(driver);这块出错是什么意思啊?和什么有关系啊?怎么改啊?
请帮帮忙,谢谢大家。

解决方案 »

  1.   

    ClassNotFoundException找不到相应的类,可能是你没有导入mysql的驱动包或者驱动写错了吧
      

  2.   

    你的数据库链接是写到server.xml中还是在web.xml中,还是类中?
    如果是前两个,可能出现这样的问题,你必须通过jsp访问。
      

  3.   

    package cn.com.caseL.test;import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    import junit.framework.TestCase;public class DBConnectionTests extends TestCase {
    @SuppressWarnings("deprecation")
    public void testDbConnection() throws IOException, ClassNotFoundException,
    SQLException {

    InputStream in = this.getClass().getClassLoader().getResourceAsStream(
    "env.properties");
    Properties env = new Properties();
    env.load(in);

    String driver = env.getProperty("jdbc.driverClassName");
    System.out.println(driver);
    String url = env.getProperty("jdbc.url");
    System.out.println("4444");
    String username = env.getProperty("jdbc.username");
    String password = env.getProperty("jdbc.password");
    System.out.println("5555");
    Class.forName(driver);
    System.out.println("6666");
    Connection conn = DriverManager.getConnection(url, username, password);
    conn.setAutoCommit(false);
    System.out.println("sssssssss");
    }
    }
      

  4.   

    这是错误信息:D:\liu>ant tests
    Buildfile: build.xmlclean:
       [delete] Deleting directory D:\liu\target\classes
       [delete] Deleting directory D:\liu\target\testclasses
       [delete] Deleting directory D:\liu\target\junit-reportsbuild:
        [mkdir] Created dir: D:\liu\target\classes
        [javac] Compiling 20 source files to D:\liu\target\classes
        [javac] 注意: 某些输入文件使用了未经检查或不安全的操作。
        [javac] 注意: 要了解详细信息,请使用 -Xlint:unchecked 重新编译。
         [copy] Copying 20 files to D:\liu\target\classessetupDB:tests:
        [mkdir] Created dir: D:\liu\target\testclasses
        [mkdir] Created dir: D:\liu\target\junit-reports
        [mkdir] Created dir: D:\liu\target\junit-reports\html
        [javac] Compiling 3 source files to D:\liu\target\testclasses
         [copy] Copying 2 files to D:\liu\target\testclasses
        [junit] Running cn.com.caseL.test.DBConnectionTests
        [junit] Testsuite: cn.com.caseL.test.DBConnectionTests
        [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.11 sec
        [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.11 sec
        [junit] ------------- Standard Output ---------------
        [junit] com.mysql.jdbc.Driver
        [junit] 4444
        [junit] 5555
        [junit] ------------- ---------------- ---------------
        [junit]
        [junit] Testcase: testDbConnection took 0.016 sec
        [junit]     Caused an ERROR
        [junit] com.mysql.jdbc.Driver
        [junit] java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        [junit]     at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        [junit]     at java.security.AccessController.doPrivileged(Native Method)
        [junit]     at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        [junit]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        [junit]     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)    [junit]     at java.lang.Class.forName0(Native Method)
        [junit]     at java.lang.Class.forName(Class.java:164)
        [junit]     at cn.com.caseL.test.DBConnectionTests.testDbConnection(DBConnec
    tionTests.java:31)
        [junit]
      

  5.   

    应该是你的build.xml文件中的CLASS_PATH配置不正确。
    你的工程中用到的jar全配置进来了吗?