无法在 Eclipse 中引用 jdbc.SQLServerDriver (ClassNotFoundException)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10834515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot reference jdbc.SQLServerDriver in Eclipse (ClassNotFoundException)
提问by Pierre
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
我正在尝试将 MSSQL 驱动程序用于 JDBC。我已按照以下说明将外部 JAR 文件添加到 Eclipse Windows IDE:
- 从这里下载安装http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx(MicrosoftJDBC Driver 4.0 for SQL Server)
- 将 zip 解压缩到我在此处解压缩的文件位置 (c:\MsJDBCforSqlDriver) - 在 Eclipse Package Explorer 中右键单击您的项目 -> Build Path -> Configure Build Path -> Java Build Path -> Libraries选项卡-添加外部 JAR 文件,然后我浏览到“C:\MsJDBCforSqlDriver\sqljdbc_4.0\enu\sqljdbc4.jar”
-然后我查看成功的参考,如下图所示,包括 SQLServerDriver.class
When I try to execute Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") I get a ClassNotFound Exception as below:
当我尝试执行 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") 我得到一个 ClassNotFound 异常如下:
I tried all suggestions I could find on Google including setting the classpath in Windows Environment Variables.
我尝试了所有可以在 Google 上找到的建议,包括在 Windows 环境变量中设置类路径。
采纳答案by Pierre
I found the answer here: http://code.google.com/p/android/issues/detail?id=27490Seems like includes under Referenced Libraries is not available at runtime and I couldn't see how to change this in my version of Eclipse
我在这里找到了答案:http: //code.google.com/p/android/issues/detail?id = 27490似乎引用库下的包含在运行时不可用,我看不到如何在我的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
" 由 [email protected] 报道,2012 年 3 月 22 日主机操作系统:Windows 7 x64 SDK 工具版本:17 Eclipse 版本:3.7.2 ADT 插件版本:17.0.0v201203161636-291853 您的项目目标平台: 2,2 模拟器中运行的平台版本: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
重现步骤: 1. 创建一个使用外部 jar 的项目(在我的例子中是 android-support-v4.jar)。2. 使用 Properties - Java Build Path - Add External Jar 添加了 android-support-v4.jar。让 Activity 从 FragmentActivity 扩展。3.清理并运行项目
EXPECTED RESULTS: Application would run launching the FragmentActivity
预期结果:应用程序将运行启动 FragmentActivity
OBSERVED RESULTS: Application crashes, android-support-v4.jar is placed within "References Libraries" and thus Android can't find it at runtime.
观察结果:应用程序崩溃,android-support-v4.jar 被放置在“参考库”中,因此 Android 在运行时无法找到它。
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.
解决方案:手动在您的项目中创建一个文件夹 /libs,将 android-support-v4.jar 复制到该文件夹中,ADT 会将其放置在“Android 依赖项”下并且它可以工作。
This is the case for all external jars added using Properties - Java Build Path - Add External Jar.
这是使用属性 - Java 构建路径 - 添加外部 Jar 添加的所有外部 jar 的情况。
"
”
回答by SteveD
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.
在 Run -> Run Configurations 菜单项下,您应该找到与包含 main() 方法的类同名的 Java 应用程序条目。
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.
查看类路径选项卡。您应该会在用户条目下找到您的项目。展开它,看看是否列出了 Microsoft JDBC Jar。
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.
您用于编译的类路径不必与您为运行时选择的相同,尽管 Eclipse 通常会将您的构建路径的任何更改反映到任何启动配置。
Your spelling and case for the package and class names look correct (Java is case-sensitive on names).
您的包名和类名的拼写和大小写看起来正确(Java 名称区分大小写)。
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.
是的,学习 Java 和 Eclipse 需要付出努力——Eclipse 与其他 IDE 有很大不同,需要时间来了解它。
回答by augus1990
Adding the JDBC driver of MS SQL Server to Eclipse:
将 MS SQL Server 的 JDBC 驱动程序添加到 Eclipse:
- Download the JDBC driver and uncompress it
- Secondary click in the root of the Eclipse project and go to "Properties".
- Now go to the section "Java Build Path - Libreries" y then click "Add External JARs".
- Search the "sqljdbc.jar" file in the directory where you unpacked the downloaded driver, double click y finish pressing "ok".
- 下载JDBC驱动并解压
- 再次单击 Eclipse 项目的根目录并转到“属性”。
- 现在转到“Java 构建路径 - 库”部分,然后单击“添加外部 JAR”。
- 在解压下载驱动的目录中搜索“sqljdbc.jar”文件,双击y完成按“确定”。
回答by Ashish Devade
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
我遇到了这个问题,然后解决了这个问题,即如果你使用 Tomcat 作为你的 Web 应用程序的服务器,同样的异常会出现,因为驱动程序对项目不可用,后来我把 jar 文件(sqljdbc4-2.0.jar ) 进入 WEB-INF/lib 文件夹然后它正在工作