类路径设置,但是:java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22253551/
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
Classpath set, but: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
提问by Doug
Okay, I'm confused. My SQL Server JAR is here:
好吧,我很困惑。我的 SQL Server JAR 在这里:
Volume in drive C has no label.
Volume Serial Number is 8008-2D93
Directory of c:\temp
03/07/2014 09:38 AM <DIR> .
03/07/2014 09:38 AM <DIR> ..
03/05/2014 10:34 PM 222,417 output.exd
02/17/2012 02:45 PM 563,117 sqljdbc.jar
02/17/2012 02:45 PM 584,207 sqljdbc4.jar
3 File(s) 1,369,741 bytes
2 Dir(s) 21,865,553,920 bytes free
My Classpath is set:
我的类路径设置为:
C:\WINDOWS\system32>echo %CLASSPATH%
.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;c:\temp\sqljdbc4.jar
Its a JDBC 4.0 driver, so I shouldn't need to do this, but I've tried setting the class name.
它是一个 JDBC 4.0 驱动程序,所以我不需要这样做,但我已经尝试设置类名。
Properties connectionProps = new Properties();
connectionProps.put("user", this.jdbcUser);
connectionProps.put("password", this.jdbcPass);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(this.jdbcUrl, connectionProps);
Still I execute my program and I'm getting the error:
我仍然执行我的程序,但出现错误:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
C:\WINDOWS\system32>java -jar "C:\Users\MYUSER\Documents\NetBeansProjects\myappSource\dist\myappSource.jar" -u MYUSER -p MYPASS -j "jdbc:sqlserver://127.0.0.1\msqlserver:1433;database=MYDB"
Mar 07, 2014 9:49:54 AM filters.myapp.dao.db.DbSwitcher getDatabaseForUrl
SEVERE: jdbc:sqlserver://127.0.0.1\msqlserver:1433;database=MYDB
Mar 07, 2014 9:49:54 AM filters.myapp.dao.db.Database connect
SEVERE: null
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at filters.myapp.dao.db.Database.connect(Database.java:217)
at filters.myapp.dao.db.Database.<init>(Database.java:38)
at filters.myapp.dao.db.MssqlDb.<init>(MssqlDb.java:15)
at filters.myapp.dao.db.DbSwitcher.getDatabaseForUrl(DbSwitcher.java:14)
at filters.myapp.UserInterface.cli(UserInterface.java:76)
at filters.myapp.UserInterface.<init>(UserInterface.java:34)
at filters.myapp.UserInterface.main(UserInterface.java:46)
In case it matters, I'm running Windows 8.1. I've tried the command prompt both as an admin and as not.
如果重要,我正在运行 Windows 8.1。我已经以管理员身份和非管理员身份尝试过命令提示符。
Java info:
爪哇信息:
C:\WINDOWS\system32>java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
Any ideas?
有任何想法吗?
SQL Server is SQL Server Express 2012.
SQL Server 是 SQL Server Express 2012。
采纳答案by user3392484
If you pass -jar to java.exe then the classpath is taken from the specified Jar file's manifest; all external classpath settings (e.g. %CLASSPATH%) are ignored.
如果将 -jar 传递给 java.exe,则类路径将从指定的 Jar 文件的清单中获取;忽略所有外部类路径设置(例如 %CLASSPATH%)。
Do one of:
执行以下操作之一:
Use java -cp ...\myapp.jar MainClassName
Put sqljdbc.jar into the Class-Path field in myapp.jar's manifest.
Discover and load sqljdbc.jar programmatically.
使用 java -cp ...\myapp.jar MainClassName
将 sqljdbc.jar 放入 myapp.jar 清单中的 Class-Path 字段。
以编程方式发现并加载 sqljdbc.jar。
回答by Jay
Open a new command prompt, and try with steps as below, its not good to work inside C:\WINDOWS\system32
打开一个新的命令提示符,并按照以下步骤尝试,在 C:\WINDOWS\system32 内工作不好
The issue says the sqljdbc4.jar is not properly set in the classpath.
问题是 sqljdbc4.jar 未在类路径中正确设置。
C:\Test
set classpath=%classpath%;.;c:\temp\sqljdbc4.jar
java -jar "C:\Users\MYUSER\Documents\NetBeansProjects\myappSource\dist\myappSource.jar" -u MYUSER -p MYPASS -j "jdbc:sqlserver://127.0.0.1\msqlserver:1433;database=MYDB"
回答by MIkka Marmik
set the class path for sqljdbc4.jar
设置 sqljdbc4.jar 的类路径
CLASSPATH= D:\sqljdbc_4.0.2206.100_enu\sqljdbc_4.0\enu\sqljdbc4.jar;
if you are eclipse user remove unnecessary .jar
file sqljdbc
如果您是 eclipse 用户,请删除不必要的.jar
文件sqljdbc
回答by Christian
I got this error after updating my Eclipse STS, it turned out my server forgot all of its configuration info during the update. I had to set up the server's classpath and environment variables all over again and that fixed the issue.
更新 Eclipse STS 后出现此错误,结果我的服务器在更新期间忘记了所有配置信息。我不得不重新设置服务器的类路径和环境变量,然后解决了这个问题。
回答by Anand
copy sqljdbc4.jar under WebContent-->WEB_INF -->lib folder. This resolved my problem.
将 sqljdbc4.jar 复制到 WebContent-->WEB_INF -->lib 文件夹下。这解决了我的问题。
回答by Rolmo2
If you programme application ZK, then put sqljdbc4.jar in the folder WebContent -> WEB_INF -> lib .
如果您编写应用程序 ZK,则将 sqljdbc4.jar 放在文件夹 WebContent -> WEB_INF -> lib 中。
Reference: Anand's comment.
参考: 阿南德的评论。