Eclipse can not find the main class

advertisements

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

java -version gives

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$1.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?

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?


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