2012-07-08 93 views
1

我使用Eclipse 3.7.2在Ubuntu 12.04與OpenJDK 7的安裝Eclipse中找不到主類

Java的版本給

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) 

每當我試圖運行一個項目,我得到以下錯誤。

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. 

對於我在Windows系統上成功編譯的所有項目,都會發生這種情況。 而且,當從終端直接使用javac和java時,它可以工作。 有沒有簡單的解決方案?這是因爲OpenJDK嗎?

類代碼是:

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:我的來源是一個名爲swingtest文件夾中。在調試時,我發現Eclipse將源代碼的類路徑用作swingtest/swingtest/src/test /而不是swingtest/src/test /。這可以糾正嗎?

+0

嘗試做一個項目 - >乾淨,然後建立工作區,並再次運行。 – zengr 2012-07-08 08:38:35

+0

是parseProfile你正在運行的類嗎?如果是這樣,請發佈其代碼。 – Tomer 2012-07-08 08:44:41

+0

你的班級是否真的沒有包? – 2012-07-08 08:46:16

回答

-1

右鍵單擊您的項目,然後選擇「構建路徑」>「配置構建路徑」。單擊源選項卡並選擇添加文件夾。然後將其指向您的swingtest文件夾。你可以刪除,Eclipse是使用

+2

事實證明,日食在我的進步中遇到了一些問題。改變了工作區,它工作正常。 – rahules 2012-07-11 17:14:05

1

可能會出現此錯誤的原因的原因如下

  1. 可能缺少的依賴/所需的JAR當前的默認。 要找到jar是否丟失,請點擊project-> Java Build Path-> source 在頂部,您可以看到錯誤消息xyz.jar丟失。 因此,通過點擊添加外部罐子選項添加特定罐子

  2. 可能是java classpath未設置。 所以配置JDK的classpath無論在Eclipse和環境也設置

  3. 嘗試清理項目,看看它的整流