java “错误:无法找到或加载主类 My.class”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11473854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:16:30  来源:igfitidea点击:

"Error: Could not find or load main class My.class"

javacommand-promptcmd

提问by James Milner

Im using Java SDK 1.7 on Windows 7 via cmd.exe . Up until a few hours ago everything was working correctly when suddenly I was unable to run my compiled class files, consistently presented with the error in the title.

我通过 cmd.exe 在 Windows 7 上使用 Java SDK 1.7。直到几个小时前,一切都正常工作,突然我无法运行我编译的类文件,标题中始终出现错误。

I seem to be able to compile my My.java file however I am unable to run the resulting class file (My.class). I am constantly given the error "Error: Could not find or load main class My.class". I have tried this with multiple other class files all resulting in the same problem.

我似乎能够编译我的 My.java 文件,但是我无法运行生成的类文件 (My.class)。我经常收到错误“错误:无法找到或加载主类 My.class”。我已经用多个其他类文件尝试过这个,都导致了同样的问题。

My 'Path' environment variable is set to "C:\Program Files (x86)\Java\jdk1.7.0_05\bin" if you were wondering

如果您想知道,我的“路径”环境变量设置为“C:\Program Files (x86)\Java\jdk1.7.0_05\bin”

I have tried reinstalling, creating and setting a classpath variable (no luck), and even directly using the

我试过重新安装、创建和设置类路径变量(没有运气),甚至直接使用

java -cp . My.class

java -cp . My.class

command.

命令。

I have tried these posts all to no avail, hence why I'm posting:

我已经尝试过这些帖子都无济于事,因此我为什么要发布:

Error: Could not find or load main class

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

Error: Could not find or load main class- Novice

错误:无法找到或加载主类 - 新手

Could not find or load main class

无法找到或加载主类

Java 1.7.0_03 Error: Could not find or load main class

Java 1.7.0_03 错误:无法找到或加载主类

If it makes any difference my code is :

如果它有任何不同,我的代码是:

import javax.swing.JOptionPane;

class My {
    public static void main(String[] args) {
       final double x = 3.2;
       int i = (int)x;
       double m = 0;
       if (x < 4) {
          String saySomething = JOptionPane.showInputDialog(i);
          System.out.println(saySomething);
        }
       else {
          String saySomething = JOptionPane.showInputDialog(i);
          System.out.println("Hello World");
        }
       while (m < 10) {
            System.out.print(" While Loop ");
            m++;
        };
       for (i=1; i < 10; i++) {
           System.out.println("For Loop");
        };

    }
}

回答by user268396

You should specify the classname instead of the file of the class to load. The difference is a simple matter of removing the .classextension.

您应该指定类名而不是要加载的类的文件。区别在于删除.class扩展名的简单问题。

回答by Peter Lawrey

I would use an IDE and you shouldn't get these issues. Compile and run is just a click of the mouse.

我会使用 IDE,你不应该遇到这些问题。只需单击鼠标即可编译和运行。

BTW to run your program from the command line

顺便说一句,从命令行运行你的程序

java -cp . My

You don't add .class

你不加 .class

回答by ioreskovic

Position yourself in a directory of your project (you need to have src and bin directories there, assuming you keep sources in src and binaries in bin)

将自己放在项目的目录中(假设您将源代码保存在 src 中,二进制文件保存在 bin 中,您需要在那里有 src 和 bin 目录)

java -cp bin My

回答by Vikas Dhyani

I myself was facing the same problem. It was happening because I was being remiss in typing the name of the class properly. In my instance, I was typing

我自己也面临着同样的问题。之所以发生这种情况,是因为我在正确键入课程名称时疏忽了。在我的例子中,我正在打字

java doubler

java倍增器

instead of

代替

java Doubler

java倍增器