Cannot reference jdbc.SQLServerDriver in Eclipse (ClassNotFoundException)

时光毁灭记忆、已成空白 提交于 2020-01-02 08:46:30

问题


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

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

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


回答1:


Under the Run -> Run Configurations menu item you should find an entry for a Java application with the same name as the class that contains your main() method.

Have a look at the classpath tab. You should find your project listed under user entries. Expand it and see if the Microsoft JDBC Jar is listed.

The class path you use for compilation need not be the same as you choose for runtime, though Eclipse usually mirrors any changes to your build path to any launch configurations.

Your spelling and case for the package and class names look correct (Java is case-sensitive on names).

And yes, learning Java and Eclipse will take effort - Eclipse is quite different from other IDEs and it takes time to get to know it.




回答2:


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 fred...@mobileinteraction.se, 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.

"




回答3:


Adding the JDBC driver of MS SQL Server to Eclipse:

  1. Download the JDBC driver and uncompress it
  2. Secondary click in the root of the Eclipse project and go to "Properties".
  3. Now go to the section "Java Build Path - Libreries" y then click "Add External JARs".
  4. Search the "sqljdbc.jar" file in the directory where you unpacked the downloaded driver, double click y finish pressing "ok".



回答4:


I faced the problem and then solved the issue i.e. If You are using the Tomcat as a server for your Web application, the same Exception was coming as the Driver was unavailable to the project, later I put the jar file (sqljdbc4-2.0.jar) into the WEB-INF/lib folder then it was working



来源:https://stackoverflow.com/questions/10834515/cannot-reference-jdbc-sqlserverdriver-in-eclipse-classnotfoundexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!