我試圖運行一個使用MySQL連接器mysql-connector-java-5.1.30-bin.jar
的編譯java類文件,但是執行失敗。在運行和Eclipse等IDE時,它工作正常。無法從命令行運行帶有mysql連接器的類
~/dropbox/workspace/mysqltut/src/zetcode/
包含以下文件:
mysql-connector-java-5.1.30-bin.jar
Version.class
Version.java
在終端導航到~/dropbox/workspace/mysqltut/src/zetcode/
後,我可以;噸弄清楚如何得到它才能正常運行。
這是我的嘗試和他們的結果。
嘗試#1
java -cp /java -cp /mysql-connector-java-5.1.30-bin.jar Version
Error: Could not find or load main class Version`
嘗試#2
java -cp .:mysql-connector-java-5.1.30-bin.jar Version
Exception in thread "main" java.lang.NoClassDefFoundError: Version (wrong name: zetcode/Version)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:
嘗試3
java -cp .;mysql-connector-java-5.1.30-bin.jar Version
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
The default VM is server,
because you are running on a server-class machine.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A : separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose:[class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
-bash: mysql-connector-java-5.1.30-bin.jar: command not found
嘗試#4
我將mysql-connector-java-5.1.30-bin
移出zetcode文件夾,並將其放在lib文件夾中一層。然後我重新導航讓我在~/dropbox/workspace/mysqltut/src/
現在跑了:
java -cp .:lib/mysql-connector-java-5.1.30-bin.jar zetcode/Version
May 02, 2014 10:54:31 PM zetcode.Version main
SEVERE: No suitable driver found for jdbc:mysql://localhost:3306/
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at zetcode.Version.main(Version.java:24)
我敢肯定的罐子包含在類路徑。我編輯了原始帖子以包含三次嘗試及其結果。請讓我知道我做錯了什麼。謝謝 – dbconfession
我加了第四次嘗試,結果是 – dbconfession