Hi guys
Iwant to connect java to oracle ,but Iam not familiarneither jdbc nor in setting classpath
however depending on my little information,I used the following code

import java.sql.*;
public class OraThin {

  public static void main(String[] args)
      throws ClassNotFoundException, SQLException
  {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    //
    // or
    // DriverManager.registerDriver
    //        (new oracle.jdbc.driver.OracleDriver());

        String url = "jdbc:oracle:thin:@computer-d219bd:1521:orcl";
         Connection conn =
         DriverManager.getConnection(url,"system", "overmars11");

    conn.setAutoCommit(false);
    Statement stmt = conn.createStatement();
    ResultSet rset =
         stmt.executeQuery("select BANNER from SYS.V_$VERSION");
    while (rset.next()) {
         System.out.println (rset.getString(1));
    }
    stmt.close();
    System.out.println ("Ok.");
  }
}

then I run the followin commands into dos command

C:\j2sdk1.4.1\bin>javac -classpath d:\oracle\product\10.2.0\db1\jdbc\lib c:\OraThin.java

C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib c:\OraThin

but I get the error:-
Exception in thread "main" java.lang.NoClassDefFoundError: c:\OraThin
Iwould be glad if someone help me.

Your have to set environment variable PATH with "C:\j2sdk1.4.1\bin>".

However you may run this program.

C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c:\;  OraThin
commented: Wrong Information -2

you may also have to include the necessary jar file.
If it is oracle 10g you can include ojdbc5.jar or ojdbc14.jar

The problem is not of PATH variables or the Oracle driver Jar not found in classpath, It is quite simply you are not invoking the program correctly.

When you are running any Java program, you give the "java" interpreter only the name of the main class (and quite obviously there is no main class by the name of C:\OraThin) .

Also in your case the complication rises cause you are specifying the value for your "classpath", here along with your other JARs you would also need to include the directory which contains the class file of your OraThin class ( which is what adatapost has done by including "c:" in the classpath as shown here d:\oracle\product\10.2.0\db1\jdbc\lib;c:\ ).

You should try what adatapost has suggested, I ended up giving him a bad cookie cause I failed to observe you are running your programs from inside the bin directory of your JDK installation. Hopefully someone will equalize for my blunder there.

Welcome stephen84s.

>but I get the error:-
Exception in thread "main" java.lang.NoClassDefFoundError: c:\OraThin
Iwould be glad if someone help me.

Have you seen this?

java launcher cannot load the class when it is qualified with path or drive letter.

C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib c:\OraThin
commented: equaliser +9

For your kind consideration try this code:

Create p1.java at c:\ and compile it

public class p1{
   public static void main(String []args) {
      System.out.println("hello");
  }
}

Now, change your current directory: Assume that c:\javaprg> is your current directory

What are the options you have to run this program?

C:\javaprg>java -cp c:\; p1

Isn't it?

Welcome stephen84s.

>but I get the error:-
Exception in thread "main" java.lang.NoClassDefFoundError: c:\OraThin
Iwould be glad if someone help me.

Have you seen this?

java launcher cannot load the class when it is qualified with path or drive letter.

C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib c:\OraThin

Yes, you're right. Java launcher can't parse a class given with path or drive. Qualification of a Java class is allowed only with package/sub-package name(s) and these packages/sub-packages, as you would certainly be aware of, are represented by the directories/sub-directories with the corresponding names.

CLASSPATH is required to be setup (or to be mentioned on the command line), as you have shown with an example, so that the class-loader knows which all root directories (and/or JARs), it will look for finding (and subsequently loading) all the qualified/non-qualified Java classes referenced by the program.

The reason for 'java.lang.NoClassDefFoundError' is that the launcher can't find a Java class with the name 'c:\OraThin' as it would not treat 'c:\' as the path qualifier of the .class file named 'OraThin.class' instead it would look for a .class file with the name represented by the entire string (including drive letter, colon, and back-slash).

Please let me know if I misunderstood your point.

Thank you all for your helpfull replies.

Your have to set environment variable PATH with "C:\j2sdk1.4.1\bin>".

ok I went to Start -> Control Panel -> System -> Advanced -> Environment Variables

variable name is java_home of course.

but what the variable value?
is it
%SystemRoot%\system32;%SystemRoot%;c:\jdk1.4.2\bin\;
or
c:\jdk1.4.2\bin\;

Thank you all for your helpfull replies.

ok I went to Start -> Control Panel -> System -> Advanced -> Environment Variables

variable name is java_home of course.

but what the variable value?
is it
%SystemRoot%\system32;%SystemRoot%;c:\jdk1.4.2\bin\;
or
c:\jdk1.4.2\bin\;

Your JAVA_HOME would probably be 'c:\jdk1.4.2' as it's normally the root of your JDK installation. BTW, you don't really need to set this environment variable. It's normally used when you have more than one JDK installation and you don't want to make your CLASSPATH and PATH settings being dependent upon the root directories of those installations.

You would just change the JAVA_HOME and everything else would remain unchanged (considering that the directory structure is the same in all the installations). In case you have only one JDK installation, you would probably not mind giving the full path wherever required.

Steps to update your PATH variable with the full path of your JDK bin:-

1. Go to the 'Environment Variables' section (the same way as you have mentioned)
2. Select 'PATH'
3. Edit
4. append 'c:\jdk1.4.2\bin' to the existing value. Separator to be used would be ';' (for Windows). For example: if the existing value of PATH is 'c:\abc;d:\a1;c:\def\ghi' then the updated value should be 'c:\abc;d:\a1;c:\def\ghi;c:\jdk1.4.2\bin'.

Setting the JDK's bin in PATH would ensure that you can invoke the tools like javac (Java Compiler), java (Java Interpreter), and other tools available in the JDK bin directory from any working directory on the system.

You will need to setup PATH even if you have set up JAVA_HOME. It's just that the PATH entry will get little trimmed as you can then setup your PATH by appending ';%JAVA_HOME%\bin' (a variable is required to be enclosed within a pair of '%'s for its value to be picked) to the existing value of the PATH. Hope this helps.

However you may run this program.

C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c:\;  OraThin

these my trials all of them not successed
C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c\; jdbc\lib;c;\; OraThin

Exception in thread "main" java.lang.NoClassDefFoundError: jdbc\lib;c;\;

C:\j2sdk1.4.1\bin>set classpath=d:\oracle\product\10.2.0\db1\jdbc\lib;c\; jdbc\lib;c;\; OraThin


C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c\; jdbc\lib;c;\; OraThin

Exception in thread "main" java.lang.NoClassDefFoundError: jdbc\lib;c;\;

C:\j2sdk1.4.1\bin>java -classpath d:\oracle\product\10.2.0\db1\jdbc\lib;c\; OraThin

Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.
OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:140)
at OraThin.main(OraThin.java:8)
Do you have suggestion
thank you.

For your kind consideration try this code:

Create p1.java at c:\ and compile it

public class p1{
   public static void main(String []args) {
      System.out.println("hello");
  }
}

Now, change your current directory: Assume that c:\javaprg> is your current directory

What are the options you have to run this program?

C:\javaprg>java -cp c:\; p1

Isn't it?

Ok It's true.

Your JAVA_HOME would probably be 'c:\jdk1.4.2' as it's normally the root of your JDK installation. BTW, you don't really need to set this environment variable. It's normally used when you have more than one JDK installation and you don't want to make your CLASSPATH and PATH settings being dependent upon the root directories of those installations.

You would just change the JAVA_HOME and everything else would remain unchanged (considering that the directory structure is the same in all the installations). In case you have only one JDK installation, you would probably not mind giving the full path wherever required.

Steps to update your PATH variable with the full path of your JDK bin:-

1. Go to the 'Environment Variables' section (the same way as you have mentioned)
2. Select 'PATH'
3. Edit
4. append 'c:\jdk1.4.2\bin' to the existing value. Separator to be used would be ';' (for Windows). For example: if the existing value of PATH is 'c:\abc;d:\a1;c:\def\ghi' then the updated value should be 'c:\abc;d:\a1;c:\def\ghi;c:\jdk1.4.2\bin'.

Setting the JDK's bin in PATH would ensure that you can invoke the tools like javac (Java Compiler), java (Java Interpreter), and other tools available in the JDK bin directory from any working directory on the system.

You will need to setup PATH even if you have set up JAVA_HOME. It's just that the PATH entry will get little trimmed as you can then setup your PATH by appending ';%JAVA_HOME%\bin' (a variable is required to be enclosed within a pair of '%'s for its value to be picked) to the existing value of the PATH. Hope this helps.

thank you dear Geek for your valuable explaniation.
I went to the path variable in enviroment variable
the value of the path variable is
D:\oracle\product\10.2.0\db_1\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Intel\DMIX
May the reason that Ihave connected oracle to ojdbc via control panel previously?
and do you suggest to me to change the current path value to the ' %j2sdk1.4.2%/BIN '?
thanks again

Your JAVA_HOME would probably be 'c:\jdk1.4.2' as it's normally the root of your JDK installation. BTW, you don't really need to set this environment variable. It's normally used when you have more than one JDK installation and you don't want to make your CLASSPATH and PATH settings being dependent upon the root directories of those installations.

You would just change the JAVA_HOME and everything else would remain unchanged (considering that the directory structure is the same in all the installations). In case you have only one JDK installation, you would probably not mind giving the full path wherever required.

Steps to update your PATH variable with the full path of your JDK bin:-

1. Go to the 'Environment Variables' section (the same way as you have mentioned)
2. Select 'PATH'
3. Edit
4. append 'c:\jdk1.4.2\bin' to the existing value. Separator to be used would be ';' (for Windows). For example: if the existing value of PATH is 'c:\abc;d:\a1;c:\def\ghi' then the updated value should be 'c:\abc;d:\a1;c:\def\ghi;c:\jdk1.4.2\bin'.

Setting the JDK's bin in PATH would ensure that you can invoke the tools like javac (Java Compiler), java (Java Interpreter), and other tools available in the JDK bin directory from any working directory on the system.

You will need to setup PATH even if you have set up JAVA_HOME. It's just that the PATH entry will get little trimmed as you can then setup your PATH by appending ';%JAVA_HOME%\bin' (a variable is required to be enclosed within a pair of '%'s for its value to be picked) to the existing value of the PATH. Hope this helps.

thank you dear Geek for your valuable explaniation.
I went to the path variable in enviroment variable
the value of the path variable is
D:\oracle\product\10.2.0\db_1\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Intel\DMIX
May the reason that Ihave connected oracle to ojdbc via control panel previously?
and do you suggest to me to change the current path value to the ' %j2sdk1.4.2%/BIN '?
thanks again.

Now when run the command
C:\j2sdk1.4.1\bin>java - cp d:\oracle\product\10.2.0\db1\jdbc\lib OraThin
unrecognize option:-
Could not recognize the java virtual machine
Waiting for your help please.

Please answer the following:

1. Can your show a path of OraThin.java?
2. Have you compile your program? if yes then show me a command you wrote at your side.

Please answer the following:

1. Can your show a path of OraThin.java?
2. Have you compile your program? if yes then show me a command you wrote at your side.

thank you for your response
I have saved OraThin.java into three paths:
c:\OraThin.java
D:\OraThin.java
c:\j2sdk1.4.1\bin\OraThin.java
At the first two cases I used Textpad tools tools to complie the class,
At the last case I compile it using the following command:-
c:\j2sdk1.4.1\bin>javac OraThin.java
May be Iwas confused and used the following command before compilation:-
c:\j2sdk1.4.1\bin>java -cp OraThin.java

Do you suggest anything?
regards.

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.