标题翻译
Couldn't find or load main class when running java class in command prompt
问题
我在Java中编写了一个简单的程序HelloWorld。但是当我尝试在命令提示符中运行我的Java代码时,
java HelloWorld
它返回
找不到或加载主类
我该怎么办??
我的代码中没有语法错误!!
英文翻译
I have written a simple program HelloWorld in java. But when I try to run my java code in command prompt using
java HelloWorld
It returns
Couldn't find or load main class
What should I do??
There's no syntax error in my code!!
答案1
得分: -1
在环境变量中将"."添加到CLASSPATH变量解决了我的问题!!
Java在CLASSPATH变量中提到的路径中搜索类,如果你不在那里添加".",它就不会在当前工作目录中搜索类!!
前往控制面板 > 系统与安全 > 系统 > 高级系统设置 > 高级
点击环境变量
如果在用户变量下存在CLASSPATH变量,请添加"."并以分号分隔。例如,如果Java安装在C:\Program Files\Java\jdk1.8.0_25\bin中,CLASSPATH将是
C:\Program Files\Java\jdk1.8.0_25\bin;.;
点击确定。
英文翻译
Adding . to CLASSPATH variable in Environment variables solved my problem!!
Java searches for the classes in the paths mentioned in CLASSPATH variable, if you don't add . there, it won't search for classes in current working directory!!!
Go to Control panel > System and Security > System > Advanced System Settings > Advanced
Click Environment Variables
If CLASSPATH variable is present under User variables , add '.' separated by semicolon. For example if java is installed in C:\Program Files\Java\jdk1.8.0_25\bin , CLASSPATH will be
C:\Program Files\Java\jdk1.8.0_25\bin;.;
Click OK.
专注分享java语言的经验与见解,让所有开发者获益!
评论