Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Replace the colon with a semicolon:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...