我正在尝试运行使用MySQL连接器mysql-connector-java-5.1.30-bin.jar
的已编译java类文件,但执行仍然失败。它在运行和IDE(例如eclipse)时工作正常。
~/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)
答案 0 :(得分:1)
您需要在类路径中包含jar。将类路径指向目录将使ClassLoader在该目录中查找类,但它不会看到这些jar并查看那些。类路径中的每个元素都是一个根,类加载器将在该根下扫描类和资源。 JRE ClassLoaders不支持嵌套的类路径根。
答案 1 :(得分:0)
试试这个,
set classpath="mysql jar location";.;
javac fileName.java