在我的_Mathematics
包中。我已經分離的源文件到bin
和src
文件夾,像這樣:分類文件和源文件的問題
_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;
?
我的代碼有什麼問題?
幫助將不勝感激。
'bin'在這裏不是一個包(或者至少不應該是)。所以它不應該出現在包名稱中(在你的導入中)。您應該確保bin目錄出現在傳遞給'javac'的classpath參數中。 –
線程「main」中的異常只是表示它的「主」線程無法找到一個特定的類,它可能是任何線程,所以只是不用擔心,並試圖確保編譯過程中給出的路徑是正確的。 – Sanchit
使用Eclipse或Netbeans之類的IDE可以幫助您避免這種類型的問題。 –