英文:
"Error: Could not find or load main class Example" when I add "package"
问题
我正在从零开始学习Java,并希望有人能帮我解决一个令人恼火的问题。以下是在我运行了"javac Example"和"java Example"之后正常工作的源代码:
class Example {
public static void main(String args[]) {
System.out.println("This is an example class");
}
}
但是,如果我尝试:
package examples;
class Example {
public static void main(String args[]) {
System.out.println("This is an example class");
}
}
...它就会出错,我会得到"错误:找不到或加载主类Example
原因:java.lang.ClassNotFoundException: Example" 的返回。
我在网络上搜索了许多资源,它们都关于正确设置类路径的同一事项(截至我输入这些文字时,我已经多次检查过),但没有一个解决方案起作用。如果可以的话,请提供帮助。谢谢。
英文:
I'm learning Java from the ground up, and I'm hoping someone can help me with an infuriating problem I've encountered. Here's the source code that works after I run "javac Example", and "java Example":
class Example {
public static void main (String args[]){
System.out.println("This is an example class");
}
}
But if I try:
package examples;
class Example {
public static void main (String args[]){
System.out.println("This is an example class");
}
}
...it goes to hell, and I get
"Error: Could not find or load main class Example
Caused by: java.lang.ClassNotFoundException: Example" returned to me.
I've trawled many resources on the net, which all say the same thing about getting the classpath right (which I've been over many times as of this typing), and none of the solutions work. Please assist if you can. Thanks.
答案1
得分: -1
当我刚开始学习时,我使用Eclipse来编写Java代码并运行程序。您需要进行编译,而Eclipse会为您完成这些操作。所以尝试使用Eclipse吧。这是一个免费的建议。
或者您可以点击以下链接获取帮助:
https://javarevisited.blogspot.com/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html
英文:
When I was a beginner I used Eclipse to code in java and run programs. You have to compile, Eclipse do it for you. So try using Eclipse. That's a free advice.
Or else here is a link to help you :
https://javarevisited.blogspot.com/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html
专注分享java语言的经验与见解,让所有开发者获益!
评论