When I run code that uses a sql driver for jbdc it works on eclipse. This is after I went into the project properties and added the external 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
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.
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. 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. All those that come after the class name are considered arguments to the main
method in the class.
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
.
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".
So always remember to pass all JVM arguments, including the classpath, before the name of the class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.