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

你离开我真会死。 提交于 2019-12-21 11:30:19

问题


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?


回答1:


Replace the colon with a semicolon:

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



回答2:


is there a typo:

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

maybe it would work if you type this:

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



回答3:


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.




回答4:


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!




回答5:


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...)



来源:https://stackoverflow.com/questions/495893/running-a-java-program-at-the-command-line-what-am-i-doing-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!