问题
My java file jdbc11.java was compiled successfully with javac jdbc11.java
command in cmd, after that when I tried to to run java jdbc11
I got this exception:
java.lang.ClassNotFoundException: com.mysql.jdbc:Driver
refering to this code in the file
Class.forName("com.mysql.jdbc.Driver");
, when I tried it in eclipse ,I added to "Java Build Path" the external jar : mysql-connector-java-5.1.20-bin.jar
and it run successfully .
in case I didn't fix it with eclipse what should I done in the first try with java jdbc11
command in order to make work ?
note: the jar in the same dir with the jdbc11.java
回答1:
Try adding the mysql-connector jar to the classpath when you execute your command-line code:
java -cp mysql-connector-java-5.1.20-bin.jar;. jdbc11
http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html
回答2:
You have to add -classpath in the execute command
java -classpath mysql-connector-java-5.1.20-bin.jar jdbc11
回答3:
If I'm understanding you allright, what you are missing is a -cp option in your java command line, which is what eclispe would do internally if you were to put it in the build path.
来源:https://stackoverflow.com/questions/14024151/how-to-add-external-jar-without-eclipse-build-path-option