在编译后执行 “java HelloWorld” 时出现错误。

huangapple go评论65阅读模式
英文:

Error in "java HelloWorld" after compiling

问题

以下是翻译好的内容:

我是一个初学者,正在按照cs61b的说明在Windows 10上设置我的Java环境。

public class HelloWorld {
     public static void main(String[] args) {
         System.out.println("Hello world!");
     }
}

在我使用 javac HelloWorld.java 编译之后,我运行了 java HelloWorld,但是报告了一个错误:

错误: 找不到或无法加载主类 HelloWorld
原因: java.lang.ClassNotFoundException: HelloWorld

相反,java HelloWorld.java 是可以运行的。我想知道如何解决这个问题?谢谢!

在编译后执行 “java HelloWorld” 时出现错误。

英文:

I am a beginner and am setting up my java on windows 10 following the instruction of cs61b

public class HelloWorld {
     public static void main(String[] args) {
         System.out.println("Hello world!");
     }
}

After I compiled it with javac HelloWorld.java, I ran java HelloWorld but it reported an error:

Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld

Instead, java HelloWorld.java works. I wonder how to fix that problem? Thanks!

在编译后执行 “java HelloWorld” 时出现错误。

答案1

得分: 2

尝试使用 java -cp . HelloWorld。使用已编译的文件时,您必须指定类路径,指明类所在的位置。然后,告诉类在哪里找到您的 main 方法。

java HelloWorld.java 起作用是因为您可能在使用 Java 11 或更高版本。

来源:https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line

英文:

Try java -cp . HelloWorld. With compiled files you have to specify the classpath where your classes are. Then you tell the class where your main method is.

The java HelloWorld.java is working because you are probably using Java 11+

https://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line

答案2

得分: 1

问题在我在CLASSPATH开头添加了".;"后解决了。谢谢!

英文:

My problem is solved when I add ".;" in the beginning of the CLASSPATH. Thanks!

huangapple
  • 本文由 发表于 2020年8月29日 16:44:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63645092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定