В моем _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 отображаются в аргументе classpath, переданном 'javac'. –
Исключение из потока «main» просто указывает на то, что его «основной» поток, который не может найти конкретный класс, может быть любым потоком, поэтому просто не беспокойтесь и постарайтесь убедиться, что путь, который вы дали во время компиляции, прав. – Sanchit
Использование среды IDE, такой как Eclipse или Netbeans, поможет предотвратить эту проблему. –