Eclipse 找不到主类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11381793/
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-09-19 18:29:26  来源:igfitidea点击:

Eclipse cannot find main class

javaeclipseopenjdk

提问by rahules

I'm using Eclipse 3.7.2 on Ubuntu 12.04 with OpenJDk 7 installed

我在安装了 OpenJDK 7 的 Ubuntu 12.04 上使用 Eclipse 3.7.2

java -version gives

java -version 给出

java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.1) (6b24-1.11.1-4ubuntu3)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

And whenever I try to run a project, I'm getting the following error.

每当我尝试运行一个项目时,都会出现以下错误。

Exception in thread "main" java.lang.NoClassDefFoundError: test/Example
Caused by: java.lang.ClassNotFoundException: test.Example
    at java.net.URLClassLoader.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: test.Example. Program will exit.

This happens for all the projects, which I have compiled successfully on my Windows system. And, when using the javac and java directly from the terminal, it works. Is there an easy solution? Is this because of OpenJDK?

对于我在 Windows 系统上成功编译的所有项目,都会发生这种情况。而且,当直接从终端使用 javac 和 java 时,它可以工作。有简单的解决方案吗?这是因为 OpenJDK 吗?

The class code is :

班级代码是:

package test;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Example extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Example() {
       setTitle("Simple example");
       setSize(300, 200);
       setLocationRelativeTo(null);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Example ex = new Example();
                ex.setVisible(true);
            }
        });
    }
}

UPDATE: My source is in a folder called swingtest. While debugging, I found that Eclipse is using the classpath of the source as swingtest/swingtest/src/test/ rather than swingtest/src/test/ . Can this be corrected?

更新:我的源代码在一个名为 swingtest 的文件夹中。在调试时,我发现 Eclipse 使用源的类路径作为 swingtest/swingtest/src/test/ 而不是 swingtest/src/test/ 。这可以纠正吗?

回答by sumanta pattanayak

This error can occur because of below reason

由于以下原因,可能会发生此错误

  1. Might be missing a dependency/Required jar. To find if the jar is missing click on project->Java Build Path->source at the top you can see a error message xyz.jar is missing. So add that particular jar by clicking add external jars option

  2. May be java classpath is not set. So configure the JDK classpath both in eclipse and also environmental setup

  3. Try to clean the project and see if its rectified

  1. 可能缺少依赖项/必需的 jar。要查找 jar 是否丢失,请单击顶部的项目->Java 构建路径->源,您可以看到错误消息 xyz.jar is missing。因此,通过单击添加外部 jar 选项添加该特定 jar

  2. 可能是未设置 java 类路径。所以在eclipse和环境设置中配置JDK类路径

  3. 尝试清理项目,看看它是否得到纠正

回答by greenkode

Right-click your project and select Build Path > Configure Build Path. click the source tab and select Add Folder. then point it to your swingtest folder. you can delete the current default that eclipse is using

右键单击您的项目并选择构建路径 > 配置构建路径。单击源选项卡并选择添加文件夹。然后将其指向您的 swingtest 文件夹。您可以删除 eclipse 正在使用的当前默认值