[英]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
. 我想做的是通过使用
javac
和java
命令在命令提示符(不使用任何IDE)中编译并运行.java
源代码来运行.java
源程序,因此该程序与MySQL连接,因此每次从cmd运行该程序时,我都需要使用java
-classpath
开关指定MySQL连接器的路径。 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. 我希望它像其他程序一样简单,例如
java MySQLConnect
,它应该运行该程序。
Is there any way I can add the connector's path to environment variables of Windows that java
make use of it. 有什么方法可以将连接器的路径添加到
java
使用Windows的环境变量中。 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环境变量中创建一个新的
CLASSPATH
变量,并添加了带有文件名的连接器的绝对路径,但这没有用。
Please provide me the workaround of this Windows and Ubuntu as well. 请同时向我提供此Windows和Ubuntu的解决方法。
Thanks. 谢谢。
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
Windows:将
mysql-connector.jar
复制到C:\\Program Files\\Java\\jdk1.6.0\\jre\\lib\\ext
并将相同文件复制到C:\\Program Files\\Java\\jre1.6.0\\lib\\ext
go to My Computer -> Properties -> Advanced -> Environment Variables 转到我的电脑->属性->高级->环境变量
Set these paths 设置这些路径
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;.;
open a fresh command propmpt 打开新的命令提示符
type 类型
java -version press Enter java -version按Enter
WINDOWS 视窗
Go to My Computer -> Properties -> Advanced -> Environment Variables
转到
My Computer -> Properties -> Advanced -> Environment Variables
then find CLASSPATH variable in System variables and click on edit to add your jar file there. 然后在系统变量中找到CLASSPATH变量,然后单击编辑以在其中添加您的jar文件。
LINUX or MAC LINUX或MAC
In your shell use a variable CLASSPATH in your .bashrc or .profile to set a default class path. 在您的shell中,在.bashrc或.profile中使用变量CLASSPATH设置默认的类路径。
Set classpath=%classpath%;location
of mysql-connector
with connector file name.jar
. 用
connector file name.jar
设置classpath=%classpath%;location
mysql-connector
classpath=%classpath%;location
。 For example: 例如:
set classpath=%classpath%;D:\TECHNICAL\JAVA WORLD\JDBC\mysql-connector-java-5.1.18-bin.jar;
D:\\TECHNICAL\\JAVA WORLD\\JDBC\\
is the location of mysql-connector
. D:\\TECHNICAL\\JAVA WORLD\\JDBC\\
是mysql-connector
的位置。
To set this path go to Advanced System Settings
-> Environment variables
-> User variables
-> CLASSPATH
-> Edit
, then type set classpath=%classpath%;D:\\TECHNICAL\\JAVA WORLD\\JDBC\\mysql-connector-java-5.1.18-bin.jar;
要设置此路径,请转至
Advanced System Settings
-> Environment variables
-> User variables
-> CLASSPATH
> Edit
,然后键入set classpath=%classpath%;D:\\TECHNICAL\\JAVA WORLD\\JDBC\\mysql-connector-java-5.1.18-bin.jar;
and finally press OK
. 最后按
OK
。
I have done in this style. 我做这种风格。 After that, I got result of programs correctly.
在那之后,我正确地得到了程序的结果。 Then there is
noCLassNotFoundException
. 然后没有
noCLassNotFoundException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.