0

For my project, I'm using C++ to call a Java Class which uses NLP Natty Date Parser Library, using JNI.

Before using the Library, when I worked with a simple Java class without any library dependencies to send a string to C++ there wasn't any problem. But once I included the respective library to parse the string, I'm not getting the result that I want. I suspect that the Java class is unable to make calls to the library.

This is an example of the function I'm calling in Java

import com.joestelmach.natty.*;

public static boolean isValidCommand(String command){
    List<DateGroup> sample;
    Parser parser = new Parser(TimeZone.getDefault());
    sample = parser.parse(command);
    if(sample.get(LOCATION_OF_DATE).getDates().size() > MAX_LIMIT)
        return TOO_MANY_DATES;
    return true;
}

This is the function in C++ which calls the aforementioned Java function.

bool Wrapper::isValidCommand(string command){
if(_cls != 0) {
    jmethodID mid = _env->GetStaticMethodID(_cls,"isValidCommand","(Ljava/lang/String;)Z");
    if(mid != 0){
        jboolean data = _env->CallStaticBooleanMethod(_cls,mid,_env->NewStringUTF(command.c_str()));
        return data;
    }
}
return false;
}

I'm unsure as to how I can link the library dependencies of the Java class I'm calling. Appreciate if anyone can help. Thanks in advance.

  • Can you add debug logs to see if the call is reaching the isValidCommand(String) method in java ? – Reji Oct 23 '13 at 14:56
  • I commented out all the lines of code in the java functions,lines that were calling the library, except for the last line. i.e return true statement. And I got the result(true) in my C++ function. So that call is reaching the java function isValidCommand. – Muhammad Muneer Oct 23 '13 at 15:03
  • Call your java method that uses the library within Java itself, and debug. Are you using maven for building? – Reji Oct 23 '13 at 15:18
  • I called the method within Java itself and it works perfectly fine. But the problem only occurs when I call from C++. – Muhammad Muneer Oct 23 '13 at 15:21
  • Print the string "command" inside isValidCommand(), when called from C++ and when called from Java. See if you find any difference? – Reji Oct 23 '13 at 15:29
  • @MuhammadMuneer Remember, a return value from a Java function that internally throws an exception is undetermined. Add logic to call `CheckException`. I suspect you are getting a ClassNotFound exception. The solution for that problem is to add the required additional jars as arguments when you create the JVM – Samhain Oct 23 '13 at 18:28
  • @Samhain I am not sure to how I am suppose to add the additional jar files. Currently, I am doing it as such: "-Djava.class.path=../java;../java/lib/antlr2.7.7.jar,../java/lib/antlr-runtime-3.2.jar,../java/lib/ical4j-1.0.2,../java/lib/natty-0.7.jar"; I am basically listing out all the library dependencies And I tried printing out the command as GreenShadow mentioned and I am getting the right command printed. I am compiling the java class that I wrote as such using the command prompt: javac -cp{folder containing lib dependencies} DateParser.java – Muhammad Muneer Oct 29 '13 at 06:01

0 Answers0