Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

mysql - Set default classpath to use in `java` command in command prompt

What I'm trying to do is running a .java source by compiling and running it from command prompt (not using any IDE) using commands javac and java and the program connects with MySQL, so everytime I run the program from cmd, I need to specify path of the MySQL connector using -classpath switch of java. And entire command to run the program gets something like below:

java -class .;path/to/connector/mysql-connector.jar MySQLConnect

where I want it to be as simple as for other programs like java MySQLConnect and it should run the program.

Is there any way I can add the connector's path to environment variables of Windows that java make use of it. I already tried by creating a new CLASSPATH variable in Windows environment variables and added absolute path of the connector with file name along, but that didn't worked.

Please provide me the workaround of this Windows and Ubuntu as well.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

WIndows : Copy mysql-connector.jar to C:Program FilesJavajdk1.6.0jrelibext and copy the same file to C:Program FilesJavajre1.6.0libext

go to My Computer -> Properties -> Advanced -> Environment Variables

Set these paths

 JAVA_HOME  
 C:Program FilesJavajdk1.6.0

 PATH 
 C:Program FilesJavajdk1.6.0in;

 CLASSPATH
 .;C:Program FilesJavajre1.6.0libextmysql-connector.jar;.;

open a fresh command propmpt

type
java -version press Enter


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...