Java程序由于设置类路径而无法运行 [英] java programs not running due to seting classpath
问题描述
我想练习用Java开发数据库程序,因此我通过声明新的环境变量classpath
并将classpath设置为E:\software\installed\java\jre\lib
并将值设置为E:\software\installed\java\jre\lib
,但现在我无法运行任何程序(即使不是-database)我收到错误
i wanted to practice developing database programs in java so i set the classpath to E:\software\installed\java\jre\lib
by declaring a new environmental variable classpath
and gave value as E:\software\installed\java\jre\lib
but now i am unable to run any program(not even non-database) i get an error
Error: Could not find or load main class MysqlConnect
但是当我删除classpath
变量时,我可以运行非数据库程序.有什么可能的问题,请让我知道解决方法.我已将路径设置为E:\software\installed\java\bin
but when i delete the classpath
variable i am able to run non-database programs. what is the possible problem and please let me know the solution. i have set path to E:\software\installed\java\bin
没有任何帮助,我尝试在java中使用类路径切换没有帮助(java -cp C:\Program Files\MySQL\MySQL Server 5.5 MysqlConnect
),也没有设置路径帮助(set CLASSPATH=%CLASSPATH%:C:\Program Files\MySQL\MySQL Server 5.5:C:\Program Files\MySQL\MySQL Server 5.5\mysql-connector-java-5.1.20-bin.jar
)都给出了错误
nothing is helping i tried using class path switch in java which is not helping (java -cp C:\Program Files\MySQL\MySQL Server 5.5 MysqlConnect
) nor do setting path is helping (set CLASSPATH=%CLASSPATH%:C:\Program Files\MySQL\MySQL Server 5.5:C:\Program Files\MySQL\MySQL Server 5.5\mysql-connector-java-5.1.20-bin.jar
) all are giving the error
Error: Could not find or load main class MysqlConnect
推荐答案
您不应使用系统变量为程序定义classpath
.标准方法是使用命令行-cp
或-classpath
选项.看看java
在不带参数的情况下输出的内容.
You shouldn't be defining a classpath
for your programs using system variables. Standard way is to use the command line -cp
or -classpath
option. Take a look at what java
prints out if you run it with no arguments.
您会在 本教程中找到提到的内容> .它说:
You will find this mentioned in this tutorial. It says:
The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications. Setting the CLASSPATH can be tricky and should be performed with care
此外,请查看默认的CLASSPATH
environmetn变量如何在其中包含.
.如果您仍然决定在其中添加课程,请将.
保留给其他公民,并在;
之后添加课程,不要仅仅覆盖整个值.
Also, see how the default CLASSPATH
environmetn variable has a .
in it. If you still decide to add your classes in there, keep the .
for other citizens and add your classes after a ;
, don't just overwrite the whole value.
这篇关于Java程序由于设置类路径而无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!