“错误:无法找到或加载主类My.class” [英] "Error: Could not find or load main class My.class"

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

问题描述

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

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.

我似乎是能够编译我的My.java文件,但我无法运行生成的类文件(My.class)。我经常给出错误错误:找不到或加载主类My.class。我试过这个与其他多个类文件,所有导致相同的问题。

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.

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

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

我已经尝试重新安装,创建和设置类路径变量(没有运气),甚至直接使用java -cp。My.class命令。

I have tried reinstalling, creating and setting a classpath variable (no luck), and even directly using the "java -cp . My.class" command.

我已经试过这些帖子都没有,因此我发布的原因:

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

错误:找不到或加载主类

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

找不到或加载主类

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");
        };

    }
}


推荐答案

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

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

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

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