In my _Mathematics
package. I've separated the source files into bin
and src
folders like so:
_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...
But, when I compile the file with main()
, there is an error like so:
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)
Here's the Permutation.java
file, where main()
is located.
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");
}
}
And here is the other file I have, 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;
}
}
Should I keep the package _Mathematics.Problems.bin;
, or should I change it to package _Mathematics.Problems.src;
?
What is wrong with my code??
Help would be much appreciated.
Two issues worth mentioning:
bin
directories are normally used for executable files. This is because (generally) your OS will have an environment setting that points to these directories, so when you try to run a program, it knows where to look. When you run a Java program, Java itself is the executable (your OS needs to know where to find it). The OS doesn't need to find your actual Java class files, Java needs to find them, for which it uses a completely different environment setting (the classpath). Because of this, if you're putting Java class files in a bin directory, you're probably doing something wrong.
Secondly, your package structure (_Mathematics.Problems.bin
) should match exactly the directory structure, but it should reflect the purpose of the classes, so _Mathematics
and Problems
are reasonable parts of a package structure, but, again, bin
or src
, is not. Normally, I would create classes
and src
directories and then my package structure begins under there
So, as explained above, to fix the issue: