Classpath set, but: java.lang.ClassNotFoundExcepti

2019-01-19 22:21发布

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

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.

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)

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

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.

6条回答
在下西门庆
2楼-- · 2019-01-19 22:52

copy sqljdbc4.jar under WebContent-->WEB_INF -->lib folder. This resolved my problem.

查看更多
Juvenile、少年°
3楼-- · 2019-01-19 22:52

If you programme application ZK, then put sqljdbc4.jar in the folder WebContent -> WEB_INF -> lib .

Reference: Anand's comment.

查看更多
老娘就宠你
4楼-- · 2019-01-19 23:05

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.

Do one of:

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

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

  3. Discover and load sqljdbc.jar programmatically.

查看更多
forever°为你锁心
5楼-- · 2019-01-19 23:07

Open a new command prompt, and try with steps as below, its not good to work inside C:\WINDOWS\system32

The issue says the sqljdbc4.jar is not properly set in the classpath.

        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"
查看更多
倾城 Initia
6楼-- · 2019-01-19 23:12

set the class path for 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

查看更多
甜甜的少女心
7楼-- · 2019-01-19 23:12

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.

查看更多
登录 后发表回答