类路径设置,但是:java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver [英] Classpath set, but: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

查看:927
本文介绍了类路径设置,但是:java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我很困惑。我的SQL Server JAR在这里:

Okay, I'm confused. My SQL Server JAR is here:

     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

设置了我的类路径:

C:\WINDOWS\system32>echo %CLASSPATH%
.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;c:\temp\sqljdbc4.jar

它是一个JDBC 4.0驱动程序,所以我不需要这样做,但我已经尝试设置类名。

Its a JDBC 4.0 driver, so I shouldn't need to do this, but I've tried setting the class name.

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$1.run(Unknown Source)
        at java.net.URLClassLoader$1.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)

如果重要,我正在运行Windows 8.1。我已经尝试过作为管理员和不是的命令提示符。

In case it matters, I'm running Windows 8.1. I've tried the command prompt both as an admin and as not.

Java信息:

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)

任何想法?

SQL Server是SQL Server Express 2012。

SQL Server is SQL Server Express 2012.

推荐答案

如果将-jar传递给java.exe,那么类路径将从指定的Jar文件清单中获取;所有外部类路径设置(例如%CLASSPATH%)都将被忽略。

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.

执行以下操作之一:


  1. 使用java -cp ... \ myapp.jar MainClassName

  1. Use java -cp ...\myapp.jar MainClassName

将sqljdbc.jar放入myapp的Class-Path字段.jar的清单。

Put sqljdbc.jar into the Class-Path field in myapp.jar's manifest.

以编程方式发现并加载sqljdbc.jar。

Discover and load sqljdbc.jar programmatically.

这篇关于类路径设置,但是:java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆