1

I'm writing a C++ project and created ARFF files within C++ code, so I need to pass these files to WEKA classifiers using C++. I tried to use a system call but encountered errors. I'm going to occasionally use a system call to WEKA to get some machine learning information. First I'd like to ensure that the training model (training.model) is up to date. So, at the beginning of main(), I call:

system("\"java weka.classifiers.trees.J48 -t ML_data.arff -d training.model\"");

How can I call WEKA classifier from within C++ code?

iksemyonov
  • 4,106
  • 1
  • 22
  • 42

1 Answers1

2

I think the error is with double quotes:

system("\"java weka.classifiers.trees.J48 -t ML_data.arff -d training.model\"");
//      ^^                                                                 ^^

This piece of code should instead be as follows:

system("java weka.classifiers.trees.J48 -t ML_data.arff -d training.model");

You appear to be using \" in order to pass the double quotes into the system() call, but that's excessive and causes an error. (The syntax for escaping a double quote is correct, but you don't need to pass the additional quotes to the system() function.)

Here's a minimal test case to show what happens on Linux with the syntax you have used:

#include <cstdlib>

int main(void)
{
        system("\"ls -l\"");
}

Output:

sh: ls -l: command not found

On the other hand, the code system("ls -l"); correctly calls ls -l and displays the output in console.

Edit:

As far as your second error message goes:

Error: Could not find or load main class weka.classifiers.functions.Logistic

It's a very common problem: What does “Could not find or load main class” mean?. You need to set the classpath to point to weka.jar in your system, for example, like so (change the exact path to the one in your distribution):

system("java -cp /usr/share/java/weka.jar weka.classifiers.trees.J48 -t ML_data.arff -d training.model");

or in the command line (note the use of the export command here)

export CLASSPATH=/usr/share/java/weka.jar:$CLASSPATH

./name_of_your_cpp_executable_file

or put CLASSPATH in your ~/.bashrc:

CLASSPATH=/usr/share/java/weka.jar:$CLASSPATH

export CLASSPATH

Community
  • 1
  • 1
iksemyonov
  • 4,106
  • 1
  • 22
  • 42