I am trying to connect to SQL SERVER 2008 using a JDBC connection. I have read some older posts on this topic(SQL server 2000, SQL server 2005) and I feel there may be some minor variations and so am posting on this forum.
I am running win XP with SP 3.

I have downloaded MS Sqlserver JDBC driver 3.0 and appended the following CLASSPATH on command prompt

c:>set CLASSPATH= C:\Program Files\Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\enu\sqljdbc4.jar.
(The path and the spelling is correct).

When SQL server 2008 is up and running, I am using the following program to test a connection to the database

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.lang.*;

class TestDB
 {
    public static void main (String [] args) 
    {
        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        }
        catch(ClassNotFoundException cnfe)
        {
            System.out.println(cnfe);
        }
    }
}

It compiles fine but throws a run time exception as java.lang.classNoFfoundException.

Can somebody pls help me trouble shoot.
Thanks
Raghu

What class was not found?

What class was not found?

The detailed error message is as follows

Exception in thread "main" java.lang.NoClassDefFoundError: Test DB
Caused by java.lang.ClassNotFoundException: TestDB
at java.net.URLClassLoader$1.run<URLClassLoader.java:202>
at java.security.AccessController.doPrivileged<Native Method>
at java.net.URLClassLoader.findClass<URLClassLoader.java:190>
at java.lang.ClassLoader.loadClass<ClassLoader.java:307>
at sun.miscLauncher$AppClassLoader.loadClass<Launcher.java:301>
at java.lang.ClassLoader.loadClass<ClassLoader.java:248>
Could not find the main class : TestDB Program will exit

How/where are you executing the program?
Can you show the contents of the folder you are in and the command you are executing to get that error message.

The java command is not finding the TestDB class file.

How/where are you executing the program?
Can you show the contents of the folder you are in and the command you are executing to get that error message.

The java command is not finding the TestDB class file.

After going through your questions(answers)I realised that there is some problem, probably in the path. I am using this computer to develop JAVA programs for a while and did not have this problem till now. I just tested another program "Hello World " and it is also throwing the same error. I checked and found that a .class file gets created. The TestDB program also compiles and has a .class file in the folder.

Yesterday I was setting up the environment for the JDBC and I might have altered some things.
First of all, I created a new JAVA_HOME in the system variable and set it to my JDK. I also added a new CLASSPATH variable to point to sqljdbc4.jar. My PATH variable in the system variables points to the bin directory of JDK. Can you pls suggest what I need to do.
Thanks
Raghu

The TestDB.class file must be in the current folder when you issue this command:
java TestDB
that will start the execution of the TestDB class.
If there are errors, copy and paste the contents of the command prompt here.

You did not answer my questions:

How/where are you executing the program?
Can you show the contents of the folder you are in and the command you are executing to get that error message.

The TestDB.class file must be in the current folder when you issue this command:
java TestDB
that will start the execution of the TestDB class.
If there are errors, copy and paste the contents of the command prompt here.

You did not answer my questions:

How/where are you executing the program?
Can you show the contents of the folder you are in and the command you are executing to get that error message.

1. I am executing this program in a "TextPad". Text pad has the tools menu where there are commands to compile and run the Java programs. I also tried "javac TestDB.java" and java TestDB at the C:>Project Prompt where the source and class files are located. The error message is the same.
2. The contents of the folder are TestDB.java and TestDB.class. Many other java and .class files are there. I am attaching the print screen of the folder contents.

It should work. What is wrong with your setup?

Can you show the commandline with the error message?
To copy the screen:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

It should work. What is wrong with your setup?

Can you show the commandline with the error message?
To copy the screen:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

C:\Project>javac TestDB.java

C:\Project>java TestDB
Exception in thread "main" java.lang.NoClassDefFoundError: TestDB
Caused by: java.lang.ClassNotFoundException: TestDB
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)
Could not find the main class: TestDB. Program will exit.

C:\Project>


This is what I get.

Its a mystery to me. Enter: java -version and see what you get. Here's what I get:

D:\Norms\Norms Tools>java -version
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

Here's what I get with the java command when there is NOT a .class file:

D:\JavaDevelopment\Testing\ForumQuestions3>java asdf
Exception in thread "main" java.lang.NoClassDefFoundError: asdf
Caused by: java.lang.ClassNotFoundException: asdf
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)
Could not find the main class: asdf. Program will exit.

It should work. What is wrong with your setup?

Can you show the commandline with the error message?
To copy the screen:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

I am having this problem with all JAVA programs. Everyone is giving the same error. I have set my classpath @ command prompt as set CLASSPATH = C:\Program Files\Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\enu\sqljdbc4.jar.
Is this creating some trouble?

Its a mystery to me. Enter: java -version and see what you get. Here's what I get:


Here's what I get with the java command when there is NOT a .class file:

This is what I get


C:\Project>java -version
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

C:\Project>

How/where are you executing the program?
Can you show the contents of the folder you are in and the command you are executing to get that error message.

I guess I should have asked for ALL of the commands.

Where are you/have you set the classpath? Only on the command prompt or via the Environment variable? If only at the commmand prompt, add a ;. at the end of the paths.
The . refers to the current directory where the .class file is.

I guess I should have asked for ALL of the commands.

Where are you/have you set the classpath? Only on the command prompt or via the Environment variable? If only at the commmand prompt, add a ;. at the end of the paths.
The . refers to the current directory where the .class file is.

I have set classpath both on command line and the environment variable. Can you pls let me know the entire command that needs to be keyed in on the command line.

current CLASSPATH is

C:\Program Files\Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\enu\sqljdbc4.jar.

java -cp . TestDB

java -cp . TestDB

This is what I got.

C:\Project>java -cp.TestDB
Unrecognized option: -cp.TestDB
Could not create the Java virtual machine.

C:\Project>java-cp.TestDB
'java-cp.TestDB' is not recognized as an internal or external command,
operable program or batch file.

C:\Project>java -cp-cp requires class path specification
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)

java -cp . TestDB

I removed the CLASSPATH in the system variable. Now I am able to compile and run other programs. The TestDB though behaves strangely

At the command Prompt

C:\Project>javac TestDB.java

C:\Project>java TestDB

C:\Project>

In TextPad
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
Press any key to continue . . .

Did you ever do this and post the results:
java -version
what did you enter for this: java -cp . TestDB
It should have spaces between every word and around the .

Add the path to the jar file with the Driver to the classpath option of the java cmd:
java -cp pathtojarfile;. TestDB

Did you ever do this and post the results:
java -version
what did you enter for this: java -cp . TestDB
It should have spaces between every word and around the .

Add the path to the jar file with the Driver to the classpath option of the java cmd:
java -cp pathtojarfile;. TestDB

I did post it earlier, but am posting it again
1. C:\Project>java -version
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)


2.
C:\Project>java -cp . TestDB

C:\Project>java -cp pathtojarfile;. TestDB

C:\Project>

Did you ever do this and post the results:
java -version
what did you enter for this: java -cp . TestDB
It should have spaces between every word and around the .

Add the path to the jar file with the Driver to the classpath option of the java cmd:
java -cp pathtojarfile;. TestDB

I did post it earlier, but am posting it again
1. C:\Project>java -version
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

For the following two commands, I get back the command prompt after execution
2.
C:\Project>java -cp . TestDB

C:\Project>java -cp pathtojarfile;. TestDB

The "pathtojarfile" is where you put the path to the jar file.

The "pathtojarfile" is where you put the path to the jar file.

I am sorry, I did not understand. Can you pls elaborate with an example.
Thanks

java -cp "C:\Program Files\Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\enu\sqljdbc4.jar";. TestDB

The "pathtojarfile" is where you put the path to the jar file.

I think I got it. I get error if I append pathtojar file


C:\Project>java -classpath C:\Program Files\Microsoft SQL Server JDBC Driver 3.0
\sqljdbc_3.0\enu\sqljdbc.jar
Exception in thread "main" java.lang.NoClassDefFoundError: Files\Microsoft
Caused by: java.lang.ClassNotFoundException: Files\Microsoft
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)
Could not find the main class: Files\Microsoft. Program will exit.

C:\Project>

java -cp "C:\Program Files\Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\enu\sqljdbc4.jar";. TestDB

Thanks.
I get back command prompt when I execute as per ur instructions


C:\Project>java -cp "C:\Program Files\Microsoft SQL Server JDBC Driver 3.0\sqljd
bc_3.0\enu\sqljdbc4.jar";. TestDB

C:\Project>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.