This discussion is archived
10 Replies Latest reply: Sep 23, 2013 11:35 AM by Sami A. RSS

cant run a class

Sami A. Explorer
Currently Being Moderated

have downloaded the sqljdbc4.jar from MS site and placed it my home directory. there i also have my java file  as shown below .

I exported the CLASSPATH variable as  export CLASSPATH=.:/usr/java/lib 

i compiled the java file  as

javac -classpath . testmssql   << it created  a class file and no errorrs .

but if i run it  now i get error

bash-3.2$ java testmssql

Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

 

bash-3.2$ more testmssql.java

import java.sql.*;

class testmssql

{

public static void main(String args[]) throws Exception

{

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=TPDB","sa","xxxxxxx");

Statement st=con.createStatement();

ResultSet rs=st.executeQuery("Select count(*) from tsr_account_status");//replace your table name

while(rs.next())

{

String s1=rs.getString(1);

String s2=rs.getString(2);

System.out.println("UserID:"+s1+"Password:"+s2);

}

con.close();

}

}

 

I have tried all ways of running but no luck .:

as you can see the driver jar  file is located at right place.


as you can see the driver jar  file is located at right place.


as you can see the driver jar  file is located at right place.

(oemdb)/home1/oracle>pwd

/home1/oracle

(oemdb)/home1/oracle>ls -ltr *.jar

-rw-r--r-- 1 oracle dba 563117 Sep 19 15:57 sqljdbc.jar

-rw-r--r-- 1 oracle dba 584207 Sep 19 15:57 sqljdbc4.jar

 

(oemdb)/home1/oracle>java -classpath /home1/oracle testmssql

Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

 

(oemdb)/home1/oracle>java -classpath /home1/oracle/sqljdbc4.jar testmssql

Exception in thread "main" java.lang.NoClassDefFoundError: testmssql


(oemdb)/home1/oracle>java -classpath /home1/oracle/sqljdbc4.jar:. testmssql

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:621)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

 

 












 





















 











  • 1. Re: cant run a class
    aksarben Journeyer
    Currently Being Moderated

    There's a lot of stuff here, so maybe I'm missing it, but I don't see where sqljdbc4.jar is added to the classpath. I assume the missing class is in that jar?

  • 2. Re: cant run a class
    Sami A. Explorer
    Currently Being Moderated

    can you see it now ? 

    regards

    -rw-r--r--   1 oracle   dba       563117 Sep 19 15:57 sqljdbc.jar

    -rw-r--r--   1 oracle   dba       584207 Sep 19 15:57 sqljdbc4.jar

    -rw-r--r--   1 oracle   dba          672 Sep 19 16:02 testmssql.java

    -rw-r--r--   1 oracle   dba         1431 Sep 19 16:27 testmssql.class

    -bash-3.2$ rm testmssql.class

    -bash-3.2$

     

    -bash-3.2$ export CLASSPATH=.:/home1/oracle/sqljdbc4.jar

     

    -bash-3.2$ javac testmssql.java

     

    -bash-3.2$ java  testmssql

     

    Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file

            at java.lang.ClassLoader.defineClass1(Native Method)

            at java.lang.ClassLoader.defineClass(ClassLoader.java:621)

            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

            at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)

            at java.net.URLClassLoader.access$100(URLClassLoader.java:56)

            at java.net.URLClassLoader$1.run(URLClassLoader.java:195)

            at java.security.AccessController.doPrivileged(Native Method)

            at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

            at java.lang.ClassLoader.loadClass(ClassLoader.java:307)

            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)

            at java.lang.ClassLoader.loadClass(ClassLoader.java:252)

            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)

            at java.lang.Class.forName0(Native Method)

            at java.lang.Class.forName(Class.java:164)

            at testmssql.main(testmssql.java:6)

    -bash-3.2$

  • 3. Re: cant run a class
    rp0428 Guru
    Currently Being Moderated
    can you see it now ? 

    Can you see this now?

    Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file

    You can't use an 'old' JRE to load/execute newer class files.

     

    Either use a newer, appropriate JRE or recompile those class files with the proper 'target' command-line arguments.

  • 4. Re: cant run a class
    VaseemAhmed Newbie
    Currently Being Moderated

    Your jar is not right version.try to take correct version.

  • 5. Re: cant run a class
    Sami A. Explorer
    Currently Being Moderated

    my java version is below , so which version of java i need to be able to run sqljdbc4.jar ?

    or what command line parameters i would need to compile/run it with the existing one?

     

    thanks

     

    java version "1.5.0_51"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_51-b10)

  • 6. Re: cant run a class
    Sami A. Explorer
    Currently Being Moderated

    from MS site i got the supported versions and it says that 1.5 JRE is supported and iam using 1.5  .

    so why iam not using the right java version?

     

     

    Java and JDBC Specification Support

    JDBC Driver VersionJRE VersionsJDBC API Version
    4.01.5, 1.613.0, 4.0
    3.01.5, 1.63.0, 4.0
    2.01.5, 1.63.0, 4.
  • 7. Re: cant run a class
    gimbal2 Guru
    Currently Being Moderated

    You have Java 5 to run the application. But I wonder if that javac you are using to compile is also Java 5. Try to run javac -version to make sure.

  • 8. Re: cant run a class
    Sami A. Explorer
    Currently Being Moderated

    it is also  version 5

     

     

    -bash-3.2$ javac -version


    javac 1.5.0_51

  • 9. Re: cant run a class
    rp0428 Guru
    Currently Being Moderated

    SamiA. wrote:

     

    from MS site i got the supported versions and it says that 1.5 JRE is supported and iam using 1.5  .

    so why iam not using the right java version?

     

     

    Java and JDBC Specification Support

    JDBC Driver VersionJRE VersionsJDBC API Version
    4.0 1.5, 1.61 3.0, 4.0
    3.0 1.5, 1.6 3.0, 4.0
    2.0 1.5, 1.6 3.0, 4.

    Well that isn't what the site I found says.

    http://technet.microsoft.com/en-us/library/ms378422.aspx

    sqljdbc4.jar class library requires a Java Runtime Environment (JRE) of version 6.0 or 7.0. Using sqljdbc4.jar on JRE 5.0 will throw an exception.

    You need to use version 6 or 7.

  • 10. Re: cant run a class
    Sami A. Explorer
    Currently Being Moderated

    i installed java_ee_sdk-5_07-jdk-6u16-solaris-sparc  and that fixed the issue .

     

    thanks for all your help

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points