tags:

views:

1764

answers:

5

Note I'm running windows, the path just looks like it's linus because I typed it manually and thats how I think of paths.

I'm trying to run a java class That I have built to diagnose my connection to a databse, it references the oracle jdbc adaptor.

When I just run it without a class path:

%> java DBDiagnostics <connectionString>

I get an exception when it reaches the following line of code:

Class.forName("oracle.jdbc.pool.OracleDataSource").newInstance();

with the following exception:

java.lang.ClassNotFoundException: oracle.jdbc.pool.OracleDataSource
        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:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:169)
        at DBDiagnostics.GetConnection(DBDiagnostics.java:43)
        at DBDiagnostics.runDiagnostic(DBDiagnostics.java:29)
        at DBDiagnostics.main(DBDiagnostics.java:18)
Creating connection.
java.sql.SQLException: No suitable driver found for lskd
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
        at DBDiagnostics.GetConnection(DBDiagnostics.java:55)
        at DBDiagnostics.runDiagnostic(DBDiagnostics.java:29)
        at DBDiagnostics.main(DBDiagnostics.java:18)
Veryfying connectivity to Database
Exception in thread "main" java.lang.NullPointerException
        at DBDiagnostics.verifyTable(DBDiagnostics.java:86)
        at DBDiagnostics.verifyTable(DBDiagnostics.java:76)
        at DBDiagnostics.verifyDatabseConnectivity(DBDiagnostics.java:68)
        at DBDiagnostics.runDiagnostic(DBDiagnostics.java:36)
        at DBDiagnostics.main(DBDiagnostics.java:18)

I assume that this is because I need to include it in the classpath.

So, I tried adding it to the classpath like this:

%> java -classpath .:ojdbc6.jar DBDiagnostics <connectionString>

The the VM just says it cant find the class:

Exception in thread "main" java.lang.NoClassDefFoundError: DBDiagnostics
Caused by: java.lang.ClassNotFoundException: DBDiagnostics
        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:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: DBDiagnostics.  Program will exit.

I know this is a question I should just know the answer to, but what am I doing wrong?

+1  A: 

is there a typo:

%> java -classpath .:ojdbc6.jar DBDiagnostics <connectionString>

maybe it would work if you type this:

%> java -classpath ./ojdbc6.jar DBDiagnostics <connectionString>
Boris Pavlović
I thought the colon meant he was running on Unix/Linux.
duffymo
That's what the prompt would suggest too.
David Grant
That wouldn't help him, since his classpath spec made his VM unable to resolve the DBDiagnostics class. Correctly referring to the ojdbc6.jar wouldn't solve that.Could it be a CLASSPATH environment variable that gets overridden by specifying ojdbc6.jar by hand?
Robin
prompt is "made up" I'm runnign on windows.
Omar Kooheji
He's not commenting on the prompt, he's commenting on the `.:` as opposed to `./`
Elie
+1  A: 

Does the DBDiagnostics.class file appear in the directory from which you're launching Java? If not, the class loader won't find it.

Does the DBDiagnostics class have a package? If it does, you have to refer to the fully resolved class name, and the root of the package hierarchy has to appear in the directory from which you launch Java.

duffymo
I think DBDiagnostics is in the default package. It finds the main method in the first example, but can't find it in the second, after he adds the jar to the classpath. Something else must have changed, though, because it should work...
Outlaw Programmer
Yeah it's in the default package
Omar Kooheji
+7  A: 

Replace the colon with a semicolon:

java -classpath .;ojdbc6.jar DBDiagnostics <connectionString>
Mike Sickler
Isn't this a Windows thing, though? I thought the ':' was the delimiter on Linux...
Outlaw Programmer
He's using Windows!
David Grant
That worked a charm
Omar Kooheji
One of the worst java platform-dependant features. Why couldn't they just agree on : or ; for Windows and Linux... I was also busted by this when moving from Linux to Windows. A recent gotcha was the default charset for String, which also depends on the platform.
João da Silva
+1  A: 

Mike Sickler's answer looks right for a Windows platform. The path separator for Windows is ";", but ":" for Unix and Linux, so make sure you always use the right one!

David Grant
+1  A: 

Long shot, but is this Unix or Windows? If on Windows the class path separator should be a semi colon:-

%> java -classpath .;ojdbc6.jar DBDiagnostics <connectionString>

And of course you need to have the ojdbc6.jar file in the current directory if you don't specify any path to it. (And possibly it's dependencies as well...)

Tooony