How to add external jar without Eclipse build path option?

柔情痞子 提交于 2019-12-23 12:18:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!