LuckyLucks
asked on
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
Hi eee:
I get the below error:
java.lang.ClassNotFoundExc eption: com.microsoft.sqlserver.jd bc.SQLServ erDriver
at java.net.URLClassLoader.fi ndClass(UR LClassLoad er.java:24 1)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:516)
at sun.misc.Launcher$AppClass Loader.loa dClass(Lau ncher.java :460)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:448)
at java.lang.Class.forName1(N ative Method)
at java.lang.Class.forName(Cl ass.java:1 42)
at ExportFromMSSQL.main(Expor tFromMSSQL .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/as mith/perl/ :/usr/ucb: $HOME/bin: /usr/bin/X 11:/
sbin:/usr/java131/jre/bin: /usr/java1 31/bin:/my home/asmit h/JDBC/sql jdbc_2.0/e nu/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/sqljdb c_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/sqljdb c_2.0/enu/ help/sampl es/connect ions > javac connectURL.java
/myhome/asmith/JDBC/sqljdb c_2.0/enu/ help/sampl es/connect ions > java connectURL
java.lang.ClassNotFoundExc eption: com.microsoft.sqlserver.jd bc.SQLServ erDriver
at java.net.URLClassLoader.fi ndClass(UR LClassLoad er.java:24 1)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:516)
at sun.misc.Launcher$AppClass Loader.loa dClass(Lau ncher.java :460)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:448)
at java.lang.Class.forName1(N ative Method)
at java.lang.Class.forName(Cl ass.java:1 42)
at connectURL.main(connectURL .java:43)
My code is the sample code provided in connectURL.java i.e below:
I get the below error:
java.lang.ClassNotFoundExc
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.Class.forName1(N
at java.lang.Class.forName(Cl
at ExportFromMSSQL.main(Expor
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/as
sbin:/usr/java131/jre/bin:
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/sqljdb
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/sqljdb
/myhome/asmith/JDBC/sqljdb
java.lang.ClassNotFoundExc
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.Class.forName1(N
at java.lang.Class.forName(Cl
at connectURL.main(connectURL
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) {}
}
}
}
you need to add the location of the jar to your CLASSPATH environment variable, *not* your PATH
ASKER
ok heres the change:
1. I have removed the entry in PATH so that PATH now looks like :-
PATH=/home/asmith:/home/as mith/perl/ :/usr/ucb: $HOME/bin: /usr/bin/X 11:/
sbin:/usr/java131/jre/bin: /usr/java1 31/bin:.
2. I run as
/myhome/asmith/JDBC/sqljdb c_2.0/enu/ help/sampl es/connect ions > javac -classpath /myhome/asmith/JDBC/sqljdb c_2.0/enu/ sqljdbc.ja r connectURL.java
/myhome/asmith/JDBC/sqljdb c_2.0/enu/ help/sampl es/connect ions > java -classpath /myhome/asmith/JDBC/sqljdb c_2.0/enu/ sqljdbc.ja r connectURL
But I get the error
Exception in thread "main" java.lang.NoClassDefFoundE rror: connectURL
1. I have removed the entry in PATH so that PATH now looks like :-
PATH=/home/asmith:/home/as
sbin:/usr/java131/jre/bin:
2. I run as
/myhome/asmith/JDBC/sqljdb
/myhome/asmith/JDBC/sqljdb
But I get the error
Exception in thread "main" java.lang.NoClassDefFoundE
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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/sqljdb c_2.0/enu/ sqljdbc.ja r:. connectURL
try:
java -classpath /myhome/asmith/JDBC/sqljdb
ASKER
doesnt work, same error
ASKER
ok sorry that seemed to have solved that error,many thx, but gives me another one:
Exception in thread "main" java.lang.UnsupportedClass VersionErr or: com/microsoft/sqlserver/jd bc/SQLServ erDriver (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defi neClass0(N ative Method)
at java.lang.ClassLoader.defi neClass(Cl assLoader. java:703)
at java.security.SecureClassL oader.defi neClass(Se cureClassL oader.java :133)
at java.net.URLClassLoader.de fineClass( URLClassLo ader.java: 320)
at java.net.URLClassLoader.ac cess$400(U RLClassLoa der.java:9 3)
at java.net.URLClassLoader$Cl assFinder. run(URLCla ssLoader.j ava:678)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.net.URLClassLoader.fi ndClass(UR LClassLoad er.java:23 9)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:516)
at sun.misc.Launcher$AppClass Loader.loa dClass(Lau ncher.java :460)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:448)
at java.lang.Class.forName1(N ative Method)
at java.lang.Class.forName(Cl ass.java:1 42)
at ExportFromMSSQL.main(Expor tFromMSSQL .java:15)
Exception in thread "main" java.lang.UnsupportedClass
at java.lang.ClassLoader.defi
at java.lang.ClassLoader.defi
at java.security.SecureClassL
at java.net.URLClassLoader.de
at java.net.URLClassLoader.ac
at java.net.URLClassLoader$Cl
at java.security.AccessContro
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.Class.forName1(N
at java.lang.Class.forName(Cl
at ExportFromMSSQL.main(Expor
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.