Search code examples
sqleclipsejdbcclasspathclassnotfoundexception

Cannot reference jdbc.SQLServerDriver in Eclipse (ClassNotFoundException)


I am trying to use the MSSQL driver for JDBC. I have followed the instructions on how to add an external JAR file to the Eclipse Windows IDE as follow:

- Download the installation from here http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx (Microsoft JDBC Driver 4.0 for SQL Server)
- Extract the zip to a file location I have extracted it here (c:\MsJDBCforSqlDriver) - In Eclipse Package Explorer right click your project -> Build Path -> Configure Build Path -> Java Build Path -> Libraries Tab- Add External JAR file and then I browse to "C:\MsJDBCforSqlDriver\sqljdbc_4.0\enu\sqljdbc4.jar"
- I then view the successful reference as in image below including SQLServerDriver.class

enter image description here

When I try to execute Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") I get a ClassNotFound Exception as below:

enter image description here

I tried all suggestions I could find on Google including setting the classpath in Windows Environment Variables.


Solution

  • I found the answer here: http://code.google.com/p/android/issues/detail?id=27490 Seems like includes under Referenced Libraries is not available at runtime and I couldn't see how to change this in my version of Eclipse

    " Reported by [email protected], Mar 22, 2012 Host OS: Windows 7 x64 SDK tools version: 17 Eclipse version: 3.7.2 ADT plug-in version:17.0.0v201203161636-291853 Platform targeted by your project: 2,2 Version of the platform running in the emulator: 2.2

    STEPS TO REPRODUCE: 1. Create a project that uses external jars (in my case android-support-v4.jar ). 2. Added the android-support-v4.jar using Properties - Java Build Path - Add External Jar. Have the Activity extends from FragmentActivity. 3. Clean and run project

    EXPECTED RESULTS: Application would run launching the FragmentActivity

    OBSERVED RESULTS: Application crashes, android-support-v4.jar is placed within "References Libraries" and thus Android can't find it at runtime.

    SOLUTION: Manualy create a folder /libs within your project, copy the android-support-v4.jar to that folder and the ADT will place it under 'Android Dependencies' and it works.

    This is the case for all external jars added using Properties - Java Build Path - Add External Jar.

    "