Search code examples
javaweka

Weka DiscriminantAnalysis NoClassDefFoundError


I'm trying to use MultiClassFLDA in discriminant analysis package but I always get an error on running the code and defining a new instance of the MultiClassFLDA class

Exception in thread "main" java.lang.NoClassDefFoundError: no/uib/cipr/matrix/Vector
    at assignment2.face.tryLDA(face.java:141)
    at assignment2.Assignment2.main(Assignment2.java:106)
Caused by: java.lang.ClassNotFoundException: no.uib.cipr.matrix.Vector
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

Solution

  • It seems to be linked to some dynamic class loading in newer versions of Weka, presumably within the Weka Package Manager: that class is defined in the mtj.jar, which is bundled inside weka.jar. In that other question, an answer suggested to extract the mtj.jar and add it to your classpath.

    As I didn't have this problem with other Filters, my guess is that MultiClassFLDA is not implemented correctly: I found out is that if you use another Filter before, that specific class will get loaded:

    // Run a dummy Filter for correct initialization
    Filter f = new Standardize();
    f.setInputFormat(data);
    Filter.useFilter(new Instances("", params, 0), f); // Dummy run on empty dataset
    // Now run the MultiClassFLDA
    f = new MultiClassFLDA();
    f.setInputFormat(data);
    data = Filter.useFilter(data, f);
    

    N.B. that is a really ugly hack! I used it to be able to work. I'll edit my answer when I find the appropriate way to do it (other than extracting the jar from Weka itslef).