2013-08-25 29 views
0

在我的_Mathematics包中。我已經分離的源文件到binsrc文件夾,像這樣:分類文件和源文件的問題

_Mathematics -> 

    Formulas -> 

     src -> 

      // source files containing mathematical formulas... 
      // Factorial.java 

     bin -> 

      // Factorial.class 
      // class files containing mathematical formulas... 

    Problems -> 

     src -> 

      // Permutation.java 
      // source files containing mathematical problems... 

     bin -> 

      // Permutation.class 
      // class files containing mathematical problems... 

但是,當我編譯main()文件,有一個錯誤,像這樣:

Exception in thread "main" java.lang.NoClassDefFoundError: _Mathematics\Problems 
\bin\Permutations (wrong name: _Mathematics/Problems/bin/Permutations) 
     at java.lang.ClassLoader.defineClass1(Native Method) 
     at java.lang.ClassLoader.defineClass(ClassLoader.java:792) 
     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) 
     at java.net.URLClassLoader.access$100(URLClassLoader.java:71) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:361) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482) 

這裏的Permutation.java文件,其中main()位於。

package _Mathematics.Problems.bin; 
import _Mathematics.Formulas.bin.Factorial; 

public class Permutations { 
    public static void main(String args[]) { 
     System.out.printf("There are 10 students. Five are to be chosen and seated in a row for a picture.%nHow many linear arrangements are possible?%n" + 
      (new Factorial(10).r/new Factorial(5).r) + "%n%n"); 
     System.out.printf("How many permutations are there in the word 'permutation'?%n" + 
      new Factorial(11).r + "%n%n"); 
    } 
} 

這裏是其他的文件我也有,Factorial.java

package _Mathematics.Formulas.bin; 

public class Factorial { 
    public int o; 
    public long r; 
    public Factorial(int num) { 
     long result = 1; 
     for(int i = num; i > 0; i--) 
      result *= i; 
     this.o = num; 
     this.r = result; 
    } 
} 

我應該保持package _Mathematics.Problems.bin;,或者我應該將其更改爲package _Mathematics.Problems.src;

我的代碼有什麼問題?

幫助將不勝感激。

+1

'bin'在這裏不是一個包(或者至少不應該是)。所以它不應該出現在包名稱中(在你的導入中)。您應該確保bin目錄出現在傳遞給'javac'的classpath參數中。 –

+0

線程「main」中的異常只是表示它的「主」線程無法找到一個特定的類,它可能是任何線程,所以只是不用擔心,並試圖確保編譯過程中給出的路徑是正確的。 – Sanchit

+0

使用Eclipse或Netbeans之類的IDE可以幫助您避免這種類型的問題。 –

回答

1

兩個問題值得一提的文件夾結構:

bin目錄通常用於可執行文件。這是因爲(一般情況下)你的操作系統會有一個指向這些目錄的環境設置,所以當你試圖運行一個程序時,它知道在哪裏尋找。當你運行一個Java程序時,Java本身就是可執行文件(你的操作系統需要知道在哪裏可以找到它)。操作系統不需要找到您的實際Java類文件,Java需要找到它們,爲此它使用完全不同的環境設置(類路徑)。因此,如果您將Java類文件放在bin目錄中,那麼您可能會出錯。

其次,你的包結構()應完全匹配的目錄結構,但它應該反映出類的目的,所以_Mathematics和​​是封裝結構的合理部分,但同樣,binsrc,是不。通常情況下,我會創造classessrc目錄,然後我的包結構下有

開始所以,如上所述,來解決該問題:

  1. 確保目錄和封裝結構是相同的 您src和類
  2. 通過去除你的包結構的bin部分,這將會更容易得到 。
+0

謝謝。這是一個很好的解釋。我得到了@G V現在想說的話。 –

1

類文件,你需要保持你的程序期待

_Mathematics \問題\ BIN \排列組合

+0

你是什麼意思? –

+0

我該如何更改我的代碼? –

+0

檢查你的java文件的文件夾結構。你的類文件應該在完全相同的文件夾結構中。 –