在命令行运行java程序,我做错了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/495893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running a java program at the command line, what am I doing wrong?
提问by Omar Kooheji
NoteI'm running windows, the path just looks like it's linus because I typed it manually and thats how I think of paths.
注意我正在运行 Windows,路径看起来像是 linus,因为我手动输入了它,这就是我对路径的看法。
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.
我正在尝试运行一个我构建的用于诊断我与数据库的连接的 java 类,它引用了 oracle jdbc 适配器。
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.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:
VM 只是说它找不到类:
Exception in thread "main" java.lang.NoClassDefFoundError: DBDiagnostics
Caused by: java.lang.ClassNotFoundException: DBDiagnostics
at java.net.URLClassLoader.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?
我知道这是一个我应该知道答案的问题,但我做错了什么?
采纳答案by Mike Sickler
Replace the colon with a semicolon:
用分号替换冒号:
java -classpath .;ojdbc6.jar DBDiagnostics <connectionString>
回答by Boris Pavlovi?
is there a typo:
是否有错别字:
%> java -classpath .:ojdbc6.jar DBDiagnostics <connectionString>
maybe it would work if you type this:
如果您输入以下内容,也许它会起作用:
%> java -classpath ./ojdbc6.jar DBDiagnostics <connectionString>
回答by duffymo
Does the DBDiagnostics.class file appear in the directory from which you're launching Java? If not, the class loader won't find it.
DBDiagnostics.class 文件是否出现在您启动 Java 的目录中?如果没有,类加载器将找不到它。
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.
DBDiagnostics 类是否有包?如果是,则必须引用完全解析的类名,并且包层次结构的根必须出现在启动 Java 的目录中。
回答by David Grant
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!
Mike Sickler 的答案看起来很适合 Windows 平台。Windows 的路径分隔符是“;”,而 Unix 和 Linux 的路径分隔符是“:”,因此请确保始终使用正确的分隔符!
回答by Tooony
Long shot, but is this Unix or Windows? If on Windows the class path separator should be a semi colon:-
远射,但这是 Unix 还是 Windows?如果在 Windows 上,类路径分隔符应该是分号:-
%> 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...)
当然,如果您没有指定任何路径,则需要在当前目录中有 ojdbc6.jar 文件。(也可能是依赖关系......)