Search code examples
javaexceptionprogram-entry-pointclassnotfound

NetBeans Exception in thread "main" java.lang.NoClassDefFoundError


I have two classes in the same java file. The B_BurglerAndMatches class is the name of the java file name. The other class is Con. I really need both classes in the same file as it is an online submission. Had this error multilple times and my solution was to try to combine to a single class. However I am sure that there is a way o

package CF_B;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Con implements Comparable<Con>{
int box;
int mat;

Con(int box, int mat) {
    this.box = box;
    this.mat = mat;
}
int matches(){
    return box*mat;
}

@Override
public int compareTo(Con o) {
    return this.matches()-o.matches();
}

}
public class B_BurglerAndMatches {
    public static void main(String [] args){
        Scanner in = new Scanner(System.in);
        int space = in.nextInt();
        int numC = in.nextInt();

        ArrayList<Con> conts = new ArrayList();
        for (int i = 0; i < numC; i++) {
            Con c = new Con(in.nextInt(),in.nextInt()); // <--- error points here
            conts.add(c);
        }

    Collections.sort(conts);

    int i = conts.size()-1;
    int matches = 0;
    int temp = 0;
    int Left_boxes = 0;
    while (space > 0 && i>=0) {
            temp = space - conts.get(i).box; //no of boxes with no space
//            System.out.println("i "+i);
//            System.out.println("temp " + temp);
            if (temp < 0) {
                Left_boxes = conts.get(i).box + temp; //no of boxes to calculate
                matches = matches + (Left_boxes * conts.get(i).mat);
            } else {
                matches = matches + conts.get(i).matches();
            }
//            System.out.println("matches "+matches);
            space = space - conts.get(i).box;
            i--;
        }

    System.out.println(matches);
}
}

The Error that showed up in the console:

Exception in thread "main" java.lang.NoClassDefFoundError: CF_B/Con
at CF_B.B_BurglerAndMatches.main(B_BurglerAndMatches.java:44)
Caused by: java.lang.ClassNotFoundException: CF_B.Con
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
Java Result: 1

Tried searching many other posts about this kind of error but didn't find any that match my case. Any explanation would be much appreciated

if you want to try my code, this is the input:

7 3
5 10
2 5
3 6

Solution

  • I suspect it's due to the fact that CON is a special file on windows. Try to rename your class.