0

This is the exception i am getting:

D:\Programming\Java\bin>JAVAC Demo.java

D:\Programming\Java\bin>java Demo
Error: Could not find or load main class Demo

D:\Programming\Java\bin>java -cp . Demo
 Exception Born java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDrive
r
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:188)
        at Demo.main(Demo.java:14)

This is my code:

    // Oracle Connection Program

import java.io.*;
import java.sql.*;

public class Demo 
{
    public static void main(String[] args)throws IOException, SQLException
    {
        String inq = " insert into login values(10,'shri')" ;

        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE","system","admin");
            Statement stmt = con.createStatement();
            stmt.execute(inq);
            con.close();
        }

        catch(Exception e)
        {
            System.out.println(" Exception Born "+e);
            e.printStackTrace();
        }
    }
}

I have set the classpath to the odbc files in the oracle folder.. also copied them into the java bin folder. I've set classpath using the environment variable and also using cmd. But still the same error. I'm even connecting to the database using sql. Table is created. What is the problem? Any Hints?

3
  • use C:\Windows\System32>path and just check in command prompt that you really have path set or not ? Commented Apr 9, 2014 at 7:16
  • In the first call you don't have the current directory in your classpath; in the second you've only got that because you're overriding the environment variable with -cp. You need the combination. Commented Apr 9, 2014 at 7:22
  • @Sumeet path isn't used for the Java classpath. Commented Apr 9, 2014 at 7:22

3 Answers 3

2

When you launch a Java application you need to specify a classpath, otherwise it will not be able to find classes (like the Oracle JDBC driver).

You need to download the Oracle JDBC driver and put it somewhere on your system (say C:\oraclejdbc\ojdbc7.jar), then run Java with:

java -cp .;C:\oraclejdbc\ojdbc7.jar Demo

The -cp specifies where Java should look for the classes:

  • . means current directory
  • C:\oraclejdbc\ojdbc7.jar means: inside this jar file.

See also Setting the Class Path. That document also mentions the CLASSPATH environment variable, but in general you'd better steer clear of that.

Sign up to request clarification or add additional context in comments.

7 Comments

if i add ojdbc7.jar in the bin folder the path will be ilke this only right? java -cp . Demo
No, because it will look in the folder for classes. A .jar is not a class, it contains classes and as such it needs to be explicitly added to the classpath.
okay.. but i have only the files named ojdbc14.jar and ojdbc14_g.jar. Do i need the ojdbc7.jar?? I'm using oracle 10g and jdk 1.7
ojdbc14_g.jar is very likely a very old version, you'd better download a more recent version, and then preferably one for Java 7.
Do you know any link where i can download it?
|
0

I was facing a similar problem adding ;.; to the end of CLASSPATH worked for me. eg. C:\Oracle\product\10.1.0\Client_1\jdbc\lib\ojdbc14.jar;.;

Comments

0

The doc is clear that we shouldn't be using oracle.jdbc.driver.OracleDriver anymore but instead oracle.jdbc.OracleDriver.

Download page for the JDBC drivers is here: http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.