在命令提示符中的`java`命令中设置要使用的默认类路径 [英] Set default classpath to use in `java` command in command prompt
问题描述
我想要做的是运行一个 .java
源通过编译和运行命令提示符(不使用任何IDE)使用命令 javac
和 java
和程序连接MySQL,所以每次我从cmd运行程序,我需要指定MySQL连接器的路径 -classpath
切换 java
。运行程序的整个命令得到如下:
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
java -class .;path/to/connector/mysql-connector.jar MySQLConnect
我希望它像其他程序一样简单,例如 java MySQLConnect
它应该运行程序。
where I want it to be as simple as for other programs like java MySQLConnect
and it should run the program.
有什么办法我可以添加连接器的路径到Windows的环境变量 java
使用它。我已经尝试在Windows环境变量中创建一个新的 CLASSPATH
变量,并添加了文件名的连接器的绝对路径,但是没有成功。
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.
请提供此Windows和Ubuntu的解决方法。
Please provide me the workaround of this Windows and Ubuntu as well.
谢谢。
推荐答案
WIndows:复制 mysql-connector.jar
至 C:\Program Files \Java\jdk1.6.0\jre\lib\ext
并将同一文件复制到 C :\Program Files\Java\jre1.6.0\lib\ext
WIndows : Copy mysql-connector.jar
to C:\Program Files\Java\jdk1.6.0\jre\lib\ext
and copy the same file to C:\Program Files\Java\jre1.6.0\lib\ext
转到我的电脑 - >属性 - >高级 - >环境变量
go to My Computer -> Properties -> Advanced -> Environment Variables
设置这些路径
JAVA_HOME
C:\Program Files\Java\jdk1.6.0
PATH
C:\Program Files\Java\jdk1.6.0\bin;
CLASSPATH
.;C:\Program Files\Java\jre1.6.0\lib\ext\mysql-connector.jar;.;
打开一个新命令propmpt
open a fresh command propmpt
java -version按Enter键
type
java -version press Enter
这篇关于在命令提示符中的`java`命令中设置要使用的默认类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!