Link to home
Start Free TrialLog in
Avatar of Cosine_Consultants
Cosine_Consultants

asked on

ClassNotFoundException: oracle.jdbc.driver.OracleDriver

Hi,
I am trying to write a simple Java app using JDeveloper to connect to an Oracle database. Following is the simple code I use:

        Driver driver;
        String driverUsed = "oracle.jdbc.driver.OracleDriver";

        try {
            driver = (Driver)Class.forName(driverUsed).newInstance();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }

and this is what I get as output:
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
Loading ... oracle.jdbc.driver.OracleDriver
      at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:164)
      at win.MainFrame.buttonClose_actionPerformed(MainFrame.java:107)
      at win.MainFrame.mav$buttonClose_actionPerformed(MainFrame.java)
      at win.MainFrame$1.actionPerformed(MainFrame.java:68)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
      at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
      at java.awt.Component.processMouseEvent(Component.java:5488)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
      at java.awt.Component.processEvent(Component.java:5253)
      at java.awt.Container.processEvent(Container.java:1966)
      at java.awt.Component.dispatchEventImpl(Component.java:3955)
      at java.awt.Container.dispatchEventImpl(Container.java:2024)
      at java.awt.Component.dispatchEvent(Component.java:3803)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
      at java.awt.Container.dispatchEventImpl(Container.java:2010)
      at java.awt.Window.dispatchEventImpl(Window.java:1774)
      at java.awt.Component.dispatchEvent(Component.java:3803)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
      at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:158)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Can somebody please help?

Note that I am using the JRE 1.4.2_04. I have a CLASSPATH=CLASSPATH=C:\J2EE\jdbc\lib defined. In this directory I have the ojdbc14.jar file which is the one that came with JDeveloper and the one Oracle says to be used with JRE 1.4 and 1.5.

I also tried putting the ojdbc14.jar file in my C:\Program Files\Java\j2re1.4.2\lib\ext directory but to no avail.

Any ideas anyone?
Avatar of fargo
fargo

Is ojdbc14.jar in your application classpath?`

Moreover, the CLASSPATH should be

CLASSPATH=%CLASSPATH%;C:\J2EE\jdbc\lib
Avatar of Cosine_Consultants

ASKER

What do you refer to when you say application classpath?
where do u keep the libraries (3rd party etc.) required for your java app? Is it in location C:\J2EE\jdbc\lib?? If yes, that's the application classpath.

Moreover follow the way given in the link
http://javaalmanac.com/egs/java.sql/ConnectOracle.html

I believe, u need not to create a new instance for the driver..why?
Avatar of Mayank S
Here is a brush up on what class-path is:

http://www.mindprod.com/jgloss/classpath.html
fargo, I copied and pasted the way to connect as given in the provided link (needless to say I changed the parameters to some correct values). Still get the same error.

The funny thing is that I created a database connection using JDeveloper's wizard using the same parameters and it connected without any problems.
try adding classes11.jar and classes12.jar in your classpath
Perhaps JDeveloper's wizard automatically added those JARs to its class-path, that's why you did not face problems with it.
I tried adding classes12.zip in my classpath and nothing happened.
Can you tell us the location of the classes12.zip file and how you added it to the class-path? Also open it in Winzip and check if it actually contains the OracleDriver class (otherwise its a corrupt file).
can you crosscheck the classpath set in the environment.
echo %CLASSPATH%

paste the classpath here
If you have a 'cmd' command window open and if you change the class-path after that through My Computer -> Properties -> Advanced -> Environment variables, it will not recognize the changes in the class-path. You'll need to open another 'cmd' window.
> I tried adding classes12.zip
adding classes12.zip wont work.
you will have to add classes12.jar file
No, it would work if its a zip file too (that's how I use it).
>> No, it would work if its a zip file too (that's how I use it).
i don't think so Mayank, it should work only with classes12.jar (jar file). Not with zip file.
My classpath is:
classpath=%CLASSPATH%;C:\J2EE\jdbc\lib\ojdbc14.jar;C:\J2EE\jdbc\lib\classes12.zip

both my ojdbc14.jar and classes12.zip exist in those directories defined in the classpath.
Sorry mayank, i take my comments for zip file back

"Class files may be standalone, combined in zip files or combined in jar files. Java looks for class files in any directories mentioned in the CLASSPATH, but only searches jar and zip files if they are explicitly mentioned. A typical CLASSPATH statement might look like this: "
>> classpath=%CLASSPATH%;

That looks wierd, the %CLASSPATH% should not be there.
>>Can you tell us the location of the classes12.zip file and how you added it to the class-path? Also open it in Winzip and check if it actually contains the OracleDriver >>class (otherwise its a corrupt file).

I opened and checked it and indeed it contains the OracleDriver class.

This is really getting to the point of frustration. I'm starting to really dislike Java.
>> >> classpath=%CLASSPATH%;

>>That looks wierd, the %CLASSPATH% should not be there.

That doesn't make a difference. That simply states that the previous value CLASSPATH should be retained. It does not affect anything.
>> Can you tell us the location of the classes12.zip file and how you added it to the class-path?

Did you add them to the class-path using SET CLASSPATH=.... ? Try adding them from My Computer -> Properties -> Advanced -> Environment variables and then check (open a new command window after that).

>> I'm starting to really dislike Java.

Be patient :)
>> That doesn't make a difference. That simply states that the previous value CLASSPATH should be retained.

Yes but it does not show the full value of class-path. Anyway what I meant was can you tell what the class-path is from the My Computer -> Properties
Moreover, classpath should be in capitals like CLASSPATH
in cmd window, if you do echo %CLASSPATH%, this should give all in classpath (without %CLASSPATH%)..it should give complete paths.









I solved my problem. The problem lied in JDeveloper and not in Java per se. When I run a java program in JDeveloper apparently, it assigned a CLASSPATH of its own - overiding the one I declared in my OS. I had to look around and set my application properties accordingly in order for it to work. And the funny thing is that it now works through JDeveloper without me having any CLASSPATH defined as a system variable. Go figure.

Anyway, thank you all for your help. Keep this in mind when using JDeveloper.

ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I did set up the classpath through the command prompt and then later through the My Computer-> Properties e.t.c.

But apparently it did not work.
You were trying to make it run from the command prompt, right?
No. Since I was using JDeveloper I was trying to run it through there.
Oh well, for some reason I thought you did development using JDeveloper and were trying to run from the command prompt. Since you said "I created a database connection using JDeveloper's wizard using the same parameters and it connected without any problems.", I thought you were able to connect using JDeveloper. Wrong track all the way, sorry, there was no need for setting the classpath in the OS from the command prompt or from My Computer -> Properties.
Nevertheless when you deploy this on a production machine which will not have JDeveloper, you will have to set up the system classpath as explained :)
Sorry for the mis-communication. I just didn't see the reason to develop in one environment and execute in another since it was a simple click of a button (of course as it turned out it wasn't a simple click of a button :-). But now it is.
>> I just didn't see the reason to develop in one environment and execute in another

It depends on the organization. They might use Windows systems for development whereas the client might want to run it on a Solaris server. Plus on the deployment machine, you will typically not have the development environment, IDE, etc - you will only have the runtime for running the application because you don't have to do development on that system. BTW, why the B grade?