简体   繁体   English

Java classpath失败,但是eclipse有效

[英]Java classpath fails but eclipse works

When I run code that uses a sql driver for jbdc it works on eclipse. 当我运行将SQL驱动程序用于jbdc的代码时,它将在eclipse上运行。 This is after I went into the project properties and added the external jar. 这是在我进入项目属性并添加了外部jar之后的。 However when I run the following from the command line it fails. 但是,当我从命令行运行以下命令时,它将失败。

java -version
java version "1.8.0_25"
javac sql_stuff.java 
java sql_stuff -classpath conn.jar

java.sql.SQLException: No suitable driver found for jdbc:mysql://...

Same with -cp 与-cp相同

 javac sql_stuff.java 
 java  sql_stuff -cp conn.jar 

In Eclipse all I had to do is go project > Properties > Java Build Path > Libraries and add the jar file. 在Eclipse中,我要做的就是转到项目>属性> Java构建路径>库,然后添加jar文件。

Edit 编辑

Got it finally running with 终于有了运行

java -cp .:conn.jar sqlstuff

Java seems to need to 're-add' a class path (even though it was '.' !!!) otherwise it wouldn't find the class. Java似乎需要“重新添加”类路径(即使它是“。” !!!),否则它将找不到该类。 Also you have to use : as a separator (or sometimes ; ), god knows why. 您还必须使用:作为分隔符(有时是;),上帝知道原因。 Hopefully this will help others when they stumble across issues. 希望这对其他人偶然发现问题会有所帮助。

When you run the java command, all the "switches" (things starting with -) that come before the class name are considered arguments to the JVM. 当您运行java命令时,类名之前的所有“开关”(以-开头的东西)都被视为JVM的参数。 All those that come after the class name are considered arguments to the main method in the class. 所有在类名之后的那些都被视为类中main方法的参数。

So the command line: 因此,命令行:

java ClassName -cp conn.jar

Will be interpreted as "run with no JVM arguments, passing the array { "-cp", "conn.jar" } as args to main . 将被解释为“运行时没有JVM参数,将数组{ "-cp", "conn.jar" }作为args传递给main

While the command line: 而在命令行中:

java -cp conn.jar ClassName

Will be interpreted as "run with a classpath of conn.jar , and call main in ClassName with an empty args array". 将被解释为“使用conn.jar的类路径运行,并使用空的args数组调用ClassName main ”。

So always remember to pass all JVM arguments, including the classpath, before the name of the class. 因此,请始终记住在类名之前传递所有JVM参数,包括类路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM