permaculture playing cards*
The moose likes JDBC and the fly likes NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "NoClassDefFoundError" Watch "NoClassDefFoundError" New topic
Author

NoClassDefFoundError

Patrick Ferguson
Ranch Hand

Joined: Jun 15, 2004
Posts: 30
I know I'm beating a dead horse because everyone seems to have this problem and so have I in the past. Usually it is corrected by simply putting the correct .jar file in the classpath. Here is my code and classpath, if anyone can see an obvious problem I'm missing I would appreciate it.

private static Connection connect(String[] s)
{
Connection conn = null;
String sUrl = s[0];
String sUser = s[1];
String sPass = s[2];
try
{
System.out.println("Getting Driver...");
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
//Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//Class forName also gives an error
System.out.println("Got Driver!!");

conn = DriverManager.getConnection(sUrl,sUser,sPass);

} catch (Exception sqle) {
sqle.printStackTrace();
}
return conn;
}

C:\Oracle\product\10.1.0\Client_2\jdbc\lib\classes12.jar;C:\Oracle\product\10.1.0\Client_2\jdbc\lib\nls_charset12.jar;C:\JDeveloper\jdk\lib\rowset.jar;C:\Oracle\product\10.1.0\Client_2\jdbc\lib\classes12.zip

Any help would be greatly appreciated.
Patrick Ferguson
Ranch Hand

Joined: Jun 15, 2004
Posts: 30
Oh, and just to add, I have the project in both Eclipse and JDeveloper. It runs fine inside both IDE's, the problem is when I try to run it via command line.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Patrick,
The only thing I can think of is to double check the paths are the same in the IDE as the command line.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Srikanth Kondeti
Greenhorn

Joined: Oct 14, 2005
Posts: 2
IDEs supports inbuilt jars and which comes with installation. So those will support. When you run thru command line you may encounter problems.

So we need to know abt the jars which need to be run our application. So that set those as Environment variables.

.. Srikanth Kondeti
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Originally posted by Patrick Ferguson:
I know I'm beating a dead horse because everyone seems to have this problem and so have I in the past. Usually it is corrected by simply putting the correct .jar file in the classpath. Here is my code and classpath, if anyone can see an obvious problem I'm missing I would appreciate it.

private static Connection connect(String[] s)
{
Connection conn = null;
String sUrl = s[0];
String sUser = s[1];
String sPass = s[2];
try
{
System.out.println("Getting Driver...");
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
//Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//Class forName also gives an error
System.out.println("Got Driver!!");

conn = DriverManager.getConnection(sUrl,sUser,sPass);

} catch (Exception sqle) {
sqle.printStackTrace();
}
return conn;
}

C:\Oracle\product\10.1.0\Client_2\jdbc\lib\classes12.jar;C:\Oracle\product\10.1.0\Client_2\jdbc\lib\nls_charset12.jar;C:\JDeveloper\jdk\lib\rowset.jar;C:\Oracle\product\10.1.0\Client_2\jdbc\lib\classes12.zip

Any help would be greatly appreciated.



classes12.jar and classes12.zip - you should only have one, not both.

classes12.jar is pretty old stuff. the latest version is ojdbc14.jar.

are you trying to connect to oracle 10g? I think you'll need a newer driver.

I'm not crazy about your code. Why do you feel the need to embed the driver class that way? you're tied to oracle forever that way.

how are you running this example? is that CLASSPATH-like string an environment variable? I'd recommend not setting CLASSPATH that way. app servers and IDEs often ignore it completely. if you're using an IDE, you could be seduced into thinking that your CLASSPATH is all set because you have a system environment variable.

ClassNotFoundException means CLASSPATH to me. I'd revisit whether or not you really have that JAR in the CLASSPATH.

I might write that method this way to make it more general:



I prefer spelling out the parameters rather than passing in an array because it makes clear to clients what they need to provide and the proper order.


%
Patrick Ferguson
Ranch Hand

Joined: Jun 15, 2004
Posts: 30
I appreciate your reply, but I'm not here for a coding lesson. This is basically a hello world type app while I try to get the oracle driver loaded. My real code is much different, but the point remains that it is failing when I try to load the oracle driver.

I have tried the classes12.jar in my path, as well as the ojdbc14.jar in my path. Each by themselves. I have probably tried 20 different classpath configurations, it's more like it can't read the driver out of the file. I checked file permissions on the .jar files and everything is open.

I am using an oracle 8i database.

I have not tried the process on UNIX yet (a place I have gotten similar applications to work), that's sort of a last resort.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Run your class from the command line with the -verbose flag. This will list all the classes the class loader loads. So, if your class is on the classpath you will see a "[Loaded oracle.jdbc.driver.OracleDriver from ....]" message. If you don't see the message there is something wrong with the classpath.

If you do, and you still can't load the class, then you have a very, very odd problem.
[ October 17, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Patrick Ferguson
Ranch Hand

Joined: Jun 15, 2004
Posts: 30
I have already run with verbose and the oracle classes do NOT show, but I am 100% positive the ojdbc14.jar file is on the classpath.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So there is no "[Loaded oracle.jdbc.driver.OracleDriver from ....]" message appearing in the verbose output? Then it is (without doubt) a classpath problem.
Patrick Ferguson
Ranch Hand

Joined: Jun 15, 2004
Posts: 30
Well if it's for sure a classpath issue, do you know why it would not work when I have it in my classpath? This is a windows 2000 OS. And I KNOW I have it in my classpath, I'm looking at it right now.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

One way to verify the Class can be found on the calsspath is to use javap. Like this:

If it is found you'll see methods and stuff. otherwise it will output:


Dave
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Well, the verbose output logs each class as it is loaded by a ClassLoader. ClassLoaders can only load resources which are in the classpath. So if there is no message, the class is not on the classpath.

You can double check this another way - write a quick program to load the class as a resource (i.e. with Class.getClassLoader().getResource("/oracle/jdbc/driver/OracleDriver.class") ;) . I the URL is null, the resource is not on the classpath (this has the extra bonus that if it isn't, the toString() method of URL logs the absolute path to the resource).

What do you do if the class still can't be found?
  • Double check the classpath is as you believe it to be - the output with -verbose will list the classpath too.
  • Check the integrity of the jar file you believe the class is in. (Long shot - perhaps you've managed to corrupt it in some way?)
  • Simplify things - write a program which you can run with only that jar in the classpath (the getResource example is a good candidate)



  • (javap: a new one to me - ah well, you learn something every day)

    [ October 18, 2005: Message edited by: Paul Sturrock ]
    [ October 18, 2005: Message edited by: Paul Sturrock ]
    David O'Meara
    Rancher

    Joined: Mar 06, 2001
    Posts: 13459

    All this time and no-one thought to check the driver class.

    You should be using oracle.jdbc.OracleDriver and not oracle.jdbc.driver.OracleDriver
    Hopefully this makes everything OK.

    Dave
    Patrick Ferguson
    Ranch Hand

    Joined: Jun 15, 2004
    Posts: 30
    I've tried both oracle.java.OracleDriver and oracle.java.jdbc.OracleDriver

    I also thought about a corrupt .jar file and I'm checking on that now.

    An interesting things is that when I run the javap on oracle.java.OracleDriver the class is not found, but when I run it on oracle.java.jdbc.OracleDriver it does find it.
    Patrick Ferguson
    Ranch Hand

    Joined: Jun 15, 2004
    Posts: 30
    Correction, I typed that incorrectly. It's oracle.jdbc.OracleDriver. And that one is found.
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336

    DOH!

    I guess they must have refactored their package structure. Ah well, if nothing else an abject lesson in being careful what you put in your classpath, particularly if you end up putting a similar class in twice.
    Patrick Ferguson
    Ranch Hand

    Joined: Jun 15, 2004
    Posts: 30
    No, I used the proper one in the classpath, I typed the wrong one just now.

    It's something very odd, and if it's a classpath issue I've never seen one like this.
    Patrick Ferguson
    Ranch Hand

    Joined: Jun 15, 2004
    Posts: 30
    Okay, after 3 days of fighting this I finally got it to work with this command.

    java -classpath C:\Oracle\product\10.1.0\Client_2\jdbc\lib\ojdb
    4.jar;C:\Oracle\product\10.1.0\Client_2\jdbc\lib\orai18n.jar;. CreateSyncFiles

    I don't know if it was the orai18n.jar or the period to signify the current directory(I think it was the orai18n.jar).

    But I'm getting drunk tonight in celebration!!

    Thanks everyone who had suggestions.
     
     
    subject: NoClassDefFoundError
     
    Similar Threads
    Installed Oracle 10g in my Local system and now I am having problem connecting to it
    Oracle9i Connectivity thru wsad5.o
    java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
    Oracle 9i thru WSAD5.0 Problem!!!!!
    Servlet and Java class