Link to home
Start Free TrialLog in
Avatar of LuckyLucks
LuckyLucks

asked on

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

Hi eee:
I get the below error:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:241)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:516)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:460)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:448)
        at java.lang.Class.forName1(Native Method)
        at java.lang.Class.forName(Class.java:142)
        at ExportFromMSSQL.main(ExportFromMSSQL.java:15)

The steps I have taken to set up my env are as folliws:
1. Add the javac and the sqljdbc.jar to my environment as follows:
PATH=/home/asmith:/home/asmith/perl/:/usr/ucb:$HOME/bin:/usr/bin/X11:/
sbin:/usr/java131/jre/bin:/usr/java131/bin:/myhome/asmith/JDBC/sqljdbc_2.0/enu/sqljdbc.jar:.

Where things reside:
1. My javac resides in usr/java131/bin
2. Downloaded and installed the MS driver (http://www.microsoft.com/downloads/details.aspx?FamilyId=F914793A-6FB4-475F-9537-B8FCB776BEFD&displaylang=en) into JDBC subdir. Now the dir structure looks like below
/myhome/asmith/JDBC/sqljdbc_2.0/enu > ls -l
total 1720
drwxr-s---   5 asmith  mygrp           512 Dec 02 13:54 auth
drwxr-s---   5 asmith  mygrp           512 Dec 02 13:54 help
-rw-r-----   1 asmith   mygrp          1675 Jul 14 14:47 install.txt
-rw-r-----   1 asmith   mygrp        13986 Jul 14 14:47 license.txt
-rw-r-----   1 asmith   mygrp        11172 Jul 14 14:44 release.txt
-rw-r-----   1 asmith   mygrp        409322 Jul 18 11:29 sqljdbc.jar
-rw-r-----   1 asmith   mygrp        422699 Jul 21 11:50 sqljdbc4.jar
drwxr-s---   5 asmith   mygrp        512 Dec 02 13:54 xa

I run as
/myhome/asmith/JDBC/sqljdbc_2.0/enu/help/samples/connections > javac connectURL.java
/myhome/asmith/JDBC/sqljdbc_2.0/enu/help/samples/connections > java connectURL
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:241)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:516)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:460)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:448)
        at java.lang.Class.forName1(Native Method)
        at java.lang.Class.forName(Class.java:142)
        at connectURL.main(connectURL.java:43)


My code is the sample code provided in connectURL.java i.e below:










import java.sql.*;
 
public class connectURL {
 
        public static void main(String[] args) {
 
                // Create a variable for the connection string.
                String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
                        "databaseName=AdventureWorks;integratedSecurity=true;";
 
                // Declare the JDBC objects.
                Connection con = null;
                Statement stmt = null;
                ResultSet rs = null;
 
                try {
                        // Establish the connection.
                        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                        con = DriverManager.getConnection(connectionUrl);
 
                        // Create and execute an SQL statement that returns some data.
                        String SQL = "SELECT TOP 10 * FROM Person.Contact";
                        stmt = con.createStatement();
                        rs = stmt.executeQuery(SQL);
 
                        // Iterate through the data in the result set and display it.
                        while (rs.next()) {
                                System.out.println(rs.getString(4) + " " + rs.getString(6));
                        }
                }
 
                // Handle any errors that may have occurred.
                catch (Exception e) {
                        e.printStackTrace();
                }
 
                finally {
                        if (rs != null) try { rs.close(); } catch(Exception e) {}
                        if (stmt != null) try { stmt.close(); } catch(Exception e) {}
                        if (con != null) try { con.close(); } catch(Exception e) {}
                }
        }
}

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

you need to add the location of the jar to your CLASSPATH environment variable, *not* your PATH
Avatar of LuckyLucks
LuckyLucks

ASKER

ok heres the change:
1. I have removed the entry in PATH so that PATH now looks like :-
PATH=/home/asmith:/home/asmith/perl/:/usr/ucb:$HOME/bin:/usr/bin/X11:/
sbin:/usr/java131/jre/bin:/usr/java131/bin:.

2. I run as
/myhome/asmith/JDBC/sqljdbc_2.0/enu/help/samples/connections > javac -classpath /myhome/asmith/JDBC/sqljdbc_2.0/enu/sqljdbc.jar connectURL.java
/myhome/asmith/JDBC/sqljdbc_2.0/enu/help/samples/connections > java -classpath /myhome/asmith/JDBC/sqljdbc_2.0/enu/sqljdbc.jar connectURL

But I get the error
Exception in thread "main" java.lang.NoClassDefFoundError: connectURL
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
connectURL is a program complied in its subdir by javac compiler, why to include it in classpath?
so it can be found, the classpath is used to find all classes


try:

 java -classpath /myhome/asmith/JDBC/sqljdbc_2.0/enu/sqljdbc.jar:. connectURL
doesnt work, same error
ok sorry that seemed to have solved that error,many thx,  but gives me another one:


Exception in thread "main" java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver (Unsupported major.minor version 49.0)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:703)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:133)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:320)
        at java.net.URLClassLoader.access$400(URLClassLoader.java:93)
        at java.net.URLClassLoader$ClassFinder.run(URLClassLoader.java:678)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:239)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:516)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:460)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:448)
        at java.lang.Class.forName1(Native Method)
        at java.lang.Class.forName(Class.java:142)
        at ExportFromMSSQL.main(ExportFromMSSQL.java:15)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial