简体   繁体   English

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

[英]Java - Error: Could not find or load main class

I figured the error was due to my Path or ClassPath being incorrect, but I used Java for months with no problem as I set the Paths when I downloaded Eclipse Neon. 我发现错误是由于我的Path或ClassPath不正确造成的,但是当我下载Eclipse Neon时设置路径时,我使用Java几个月没有问题。 I also seen being in packages can cause it, so I created classes outside of packages and still got the error. 我还看到在包中会导致它,所以我在包外创建了类,但仍然收到错误。

The weird thing is some classes I create run fine, but others won't, and they'll be the exact some code in the same package, but only have different names. 奇怪的是,我创建的某些类运行良好,但其他类却无法运行,并且它们与同一包中的某些代码完全相同,但名称不同。

Here is the exact example, HelloWorld works, but parserpractice doesn't, and they are both in the exact same location: 这是确切的示例,HelloWorld可以工作,但parserpractice无效,它们都位于完全相同的位置:

package ParserPractice;

public class HelloWorld {

    public HelloWorld() {
        // TODO Auto-generated constructor stub
    }

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

    }

}

package ParserPractice;

public class parserpractice {

    public parserpractice() {
        // TODO Auto-generated constructor stub
    }

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

My Path and ClassPath environment variables on Windows 7 are: Windows 7上的My Path和ClassPath环境变量是:

Path variable name: C:\\Program Files\\Java\\jdk1.8.0_101\\bin;C:\\Program Files\\Java\\jre1.8.0_101\\bin; 路径变量名称:C:\\ Program Files \\ Java \\ jdk1.8.0_101 \\ bin; C:\\ Program Files \\ Java \\ jre1.8.0_101 \\ bin;

ClassPath variable name: C:\\Program Files\\Java\\jdk1.8.0_101\\bin ClassPath变量名称:C:\\ Program Files \\ Java \\ jdk1.8.0_101 \\ bin

I changed those around constantly, but all my old code still runs, and some new classes still run. 我不断地更改这些内容,但是所有旧代码仍在运行,并且一些新类仍在运行。 But for some reason some new classes will generate the error: 但是由于某些原因,一些新类将产生错误:

Error: Could not find or load main class. 错误:找不到或加载主类。

How do I solve this error? 我该如何解决这个错误?

Can you change your path and classpath like this 你能这样改变你的路径和类路径吗

Path variable name: %ProgramFiles%\\Java\\jdk1.8.0_101\\bin;%ProgramFiles%\\Java\\jre1.8.0_101\\bin; 路径变量名称:%ProgramFiles%\\ Java \\ jdk1.8.0_101 \\ bin;%ProgramFiles%\\ Java \\ jre1.8.0_101 \\ bin; ClassPath variable name: %ProgramFiles%\\Java\\jdk1.8.0_101\\bin ClassPath变量名称:%ProgramFiles%\\ Java \\ jdk1.8.0_101 \\ bin

I used eclipse on my laptop and it worked fine. 我在笔记本电脑上使用了eclipse,效果很好。 I think the issue has something to do with how old my PC is. 我认为问题与我的PC的年龄有关。 It's ancient. 很古老 I'm building a new one in the next couple months. 我将在接下来的几个月中建立一个新的。

So, I'm closing this question as I tried everything possible, and at this point I think it's some esoteric problem that isn't even worth trying to figure out. 因此,在尝试所有可能的方法时,我将结束这个问题,在这一点上,我认为这是一个深奥的问题,甚至不值得尝试解决。 I might try and re-install eclipse, and see if that fixes it, but since I'm getting a new PC I might not even bother. 我可能会尝试重新安装eclipse,看看是否能解决该问题,但是由于我要购买一台新PC,所以我什至都不会打扰。

Thanks for your help. 谢谢你的帮助。

You can check your path for referenced libraries. 您可以检查所引用库的路径。 Go to referenced libraries >Build path > configure build path and check whether all the paths are correct or not. 转到引用的库>构建路径>配置构建路径,然后检查所有路径是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM