获取错误“无法找到或加载主类HelloWorld” [英] Getting error "cannot find or load main class HelloWorld"

查看:166
本文介绍了获取错误“无法找到或加载主类HelloWorld”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的代码:

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

文件名HelloWorld.java

And the filename HelloWorld.java

在命令提示符下,我输入:

In command prompt, I type in:

javac HelloWorld.java

java HelloWorld

(同一目录)

我收到错误:无法找到或加载主类HelloWorld

I am getting the error: "cannot find or load main class HelloWorld"

我确定它与不正确的安装无关,因为我重新安装了jdk和jre两次。

I'm sure it has nothing to do with improper installation because I reinstalled jdk and jre twice.

编辑:

之前有效,并且第二天,没有更改代码,目录或任何内容,它开始出错。

This was working before, and the next day, no change of code, directory, or anything, its started giving an error.

推荐答案

如果您有一个错误/不适当的CLASSPATH环境变量设置;例如当前目录不在类路径上。 (默认为......)

You could get this behaviour if you have an incorrect / inappropriate setting of the CLASSPATH environment variable; e.g. the current directory isn't on the classpath. (It is by default ... )

试试这个:

java -classpath . HelloWorld

假设有效......问题是你对classpath概念的理解。 Oracle文档对此进行了解释:

Assuming that works ... the problem is your understanding of the concept of the "classpath". This is explained well by the Oracle documentation:

  • The java & javac command documentation ... particular the -classpath argument
  • Setting the Classpath.
  • The Java Tutorial - PATH and CLASSPATH

这篇关于获取错误“无法找到或加载主类HelloWorld”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆