Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a java program at the command line, what am I doing wrong?

Tags:

java

classpath

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 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?

like image 307
Omar Kooheji Avatar asked Jan 30 '09 15:01

Omar Kooheji


2 Answers

Replace the colon with a semicolon:

java -classpath .;ojdbc6.jar DBDiagnostics <connectionString>
like image 178
Mike Sickler Avatar answered Oct 22 '22 03:10

Mike Sickler


is there a typo:

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

maybe it would work if you type this:

%> java -classpath ./ojdbc6.jar DBDiagnostics <connectionString>
like image 1
Boris Pavlović Avatar answered Oct 22 '22 02:10

Boris Pavlović