Собственный интерфейс Java - C ++ не ожидает завершения функции Java - PullRequest
4 голосов
/ 24 июня 2019

Я хочу, чтобы функциональность Stanford Core NLP, написанной на Java, была доступна на C ++. Для этого я использую нативный интерфейс Java. У меня есть объект Java, который оборачивает несколько функций так, чтобы их было проще вызывать из C ++. Однако, когда я вызываю эти функции, C ++ не ждет завершения выполнения функций, прежде чем перейти к следующей.

У объекта Java есть функция Main, которую я использую для тестирования, которая вызывает все соответствующие функции для целей тестирования. Когда работает только Java, он работает отлично. Аннотация ожидает завершения установки (что занимает некоторое время), а функция, которая получает зависимости, ожидает завершения функции примечания. Прекрасно ожидаемое и правильное поведение. Проблема возникает, когда я начинаю вызывать Java-функции из C ++. Часть java-функции будет запущена, но она выйдет и вернется к C ++ в определенные моменты, указанные ниже. Я хотел бы, чтобы C ++ ждал завершения методов Java.

Если это имеет значение, я использую Stanford Core NLP 3.9.2.

Я использовал код в StanfordCoreNlpDemo.java, который поставляется с файлами NLP .jar в качестве отправной точки.

import java.io.*;
import java.util.*;

// Stanford Core NLP imports

public class StanfordCoreNLPInterface {

    Annotation annotation;
    StanfordCoreNLP pipeline;

    public StanfordCoreNLPInterface() {}

    /** setup the NLP pipeline */
    public void setup() {
        // Add in sentiment
        System.out.println("creating properties");
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, sentiment");
        System.out.println("starting the parser pipeline");
        //<---- doesn't get past this point
        pipeline = new StanfordCoreNLP(props);
        System.out.println("started the parser pipeline");
    }

    /** annotate the text */
    public void annotateText(String text) {
        // Initialize an Annotation with some text to be annotated. The text is the argument to the constructor.
        System.out.println("text");
        System.out.println(text);
        //<---- doesn't get past this point
        annotation = new Annotation(text);
        System.out.println("annotation set");
        // run all the selected annotators on this text
        pipeline.annotate(annotation);
        System.out.println("annotated");
    }

    /** print the dependencies */
    public void dependencies() {
        // An Annotation is a Map with Class keys for the linguistic analysis types.
        // You can get and use the various analyses individually.
        // For instance, this gets the parse tree of the first sentence in the text.
        List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
        if (sentences != null && ! sentences.isEmpty()) {
            CoreMap sentence = sentences.get(0);
            System.out.println("The first sentence dependencies are:");
            SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class);
            System.out.println(graph.toString(SemanticGraph.OutputFormat.LIST));
        }
    }

    /** Compile: javac -classpath stanford-corenlp-3.9.2.jar -Xlint:deprecation StanfordCoreNLPInterface.java*/
    /** Usage: java -cp .:"*" StanfordCoreNLPInterface*/
    public static void main(String[] args) throws IOException {
        System.out.println("starting main function");
        StanfordCoreNLPInterface NLPInterface = new StanfordCoreNLPInterface();
        System.out.println("new object");
        NLPInterface.setup();
        System.out.println("setup done");

        NLPInterface.annotateText("Here is some text to annotate");
        NLPInterface.dependencies();
    }
}

Я использовал код в этом уроке http://tlab.hatenablog.com/entry/2013/01/12/125702 в качестве отправной точки.

#include <jni.h>

#include <cassert>
#include <iostream>


/** Build:  g++ -Wall main.cpp -I/usr/lib/jvm/java-8-openjdk/include -I/usr/lib/jvm/java-8-openjdk/include/linux -L${LIBPATH} -ljvm*/
int main(int argc, char** argv) {
    // Establish the JVM variables
    const int kNumOptions = 3;
    JavaVMOption options[kNumOptions] = {
        { const_cast<char*>("-Xmx128m"), NULL },
        { const_cast<char*>("-verbose:gc"), NULL },
        { const_cast<char*>("-Djava.class.path=stanford-corenlp"), NULL },
        { const_cast<char*>("-cp stanford-corenlp/.:stanford-corenlp/*"), NULL }
    };

    // JVM setup before this point.
    // java object is created using env->AllocObject();
    // get the class methods
    jmethodID mid =
        env->GetStaticMethodID(cls, kMethodName, "([Ljava/lang/String;)V");
    jmethodID midSetup =
        env->GetMethodID(cls, kMethodNameSetup, "()V");
    jmethodID midAnnotate =
        env->GetMethodID(cls, kMethodNameAnnotate, "(Ljava/lang/String;)V");
    jmethodID midDependencies =
        env->GetMethodID(cls, kMethodNameDependencies, "()V");
    if (mid == NULL) {
        std::cerr << "FAILED: GetStaticMethodID" << std::endl;
        return -1;
    }
    if (midSetup == NULL) {
        std::cerr << "FAILED: GetStaticMethodID Setup" << std::endl;
        return -1;
    }
    if (midAnnotate == NULL) {
        std::cerr << "FAILED: GetStaticMethodID Annotate" << std::endl;
        return -1;
    }
    if (midDependencies == NULL) {
        std::cerr << "FAILED: GetStaticMethodID Dependencies" << std::endl;
        return -1;
    }
    std::cout << "Got all the methods" << std::endl;

    const jsize kNumArgs = 1;
    jclass string_cls = env->FindClass("java/lang/String");
    jobject initial_element = NULL;
    jobjectArray method_args = env->NewObjectArray(kNumArgs, string_cls, initial_element);

    // prepare the arguments
    jstring method_args_0 = env->NewStringUTF("Get the flask in the room.");
    env->SetObjectArrayElement(method_args, 0, method_args_0);
    std::cout << "Finished preparations" << std::endl;

    // run the function
    //env->CallStaticVoidMethod(cls, mid, method_args);
    //std::cout << "main" << std::endl;
    env->CallVoidMethod(jobj, midSetup);
    std::cout << "setup" << std::endl;
    env->CallVoidMethod(jobj, midAnnotate, method_args_0);
    std::cout << "annotate" << std::endl;
    env->CallVoidMethod(jobj, midDependencies);
    std::cout << "dependencies" << std::endl;
    jvm->DestroyJavaVM();
    std::cout << "destroyed JVM" << std::endl;

    return 0;
}

Компиляция C ++ с помощью g ++ и -Wall не дает предупреждений или ошибок, равно как и компиляция Java с использованием javac. Когда я запускаю код C ++, я получаю следующий вывод.

Got all the methods
Finished preparations
creating properties
starting the parser pipeline
setup
text
Get the flask in the room.
annotate
dependencies
destroyed JVM

Следуя указаниям и печатным строкам, начиная с C ++, вы можете увидеть, как C ++ может успешно получать методы и завершать подготовку JVM и методов перед вызовом метода установки в java. Этот метод установки запускается и вызывает первую строку печати, создает свойства и присваивает значения, затем завершает работу, прежде чем он сможет запустить конвейер синтаксического анализа и возвращается к C ++. Это в основном та же история, продвигающаяся вперед: вызывается текстовая функция аннотирования, которая успешно получает текст из вызова метода C ++, но завершает работу, прежде чем создает объект аннотации. У меня не так много отладочных printlns в зависимостях, потому что в этот момент это не имеет значения, но само собой разумеется, что ни один из существующих printlns не вызывается. В самом конце JVM уничтожается, и программа заканчивается.

Спасибо за любую помощь или понимание, которое вы можете предоставить.

1 Ответ

1 голос
/ 25 июня 2019

вызовы методов JNI всегда синхронны.Когда они возвращаются до того, как достигли конца метода, это происходит потому, что код обнаружил исключение.Это не распространяется на исключения C ++ автоматически.Вы всегда должны проверять исключения после каждого вызова.

Распространенная проблема для кода, который прекрасно работает при вызове из другого кода Java, но не при вызове с JNI, - это путь к классу виртуальной машины.В то время как java.exe разрешит * и добавит каждый соответствующий JAR в путь к классам, программы, использующие интерфейс вызова, должны сделать это сами.-Djava.class.path в JavaVMOption работает только с реальными файлами.Также вы можете использовать только фактические параметры виртуальной машины, а не аргументы, такие как -cp, потому что они также разрешаются только java.exe и не являются частью интерфейса вызова.

...