- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我希望用 java 编写的 Stanford Core NLP 的功能可以在 C++ 中使用。为此,我使用了 Java native 接口(interface)。我有一个 Java 对象,它以一种更容易从 C++ 调用的方式包装了多个函数。但是,当我确实调用这些函数时,C++ 不会等待函数完成后再转到下一个函数。
Java 对象有一个我用于测试的 Main 函数,它调用所有适当的函数来进行测试。当只运行 Java 时,它工作得很好。注解等待设置完成(这确实需要一段时间),获取依赖项的函数等待注解函数完成。完全预期和正确的行为。当我开始从 C++ 调用 java 函数时,问题就来了。部分 java 函数将运行,但它会在某些点退出并返回到 C++,如下所述。我希望 C++ 等待 java 方法完成。
如果重要的话,我使用的是 Stanford Core NLP 3.9.2。
我使用 NLP .jar 文件附带的 StanfordCoreNlpDemo.java 中的代码作为起点。
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;
}
用 g++ 和 -Wall 编译 C++ 不会给出警告或错误,用 javac 编译 Java 也不会。当我运行 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++ 的 couts 和 printlines 之后,您可以看到 C++ 如何能够在调用 java 中的设置方法之前成功获取方法并完成 JVM 和方法准备。该设置方法启动并调用第一个打印行,创建属性并分配值,然后在它可以启动解析器管道并返回到 C++ 之前退出。它基本上是相同的故事向前发展,注释文本函数被调用并成功地从 C++ 方法调用接收文本,但在创建注释对象之前退出。我在依赖项中没有那么多调试 printlns,因为那时它并不重要,但不用说,现有的 printlns 都没有被调用。最后,JVM 被销毁,程序结束。
感谢您提供的任何帮助或见解。
最佳答案
JNI 方法调用始终是同步的。当它们在到达方法末尾之前返回时,那是因为代码遇到了异常。这不会自动传播到 C++ 异常。您始终必须在每次调用后检查异常情况。
代码在从其他 Java 代码调用时运行良好但在使用 JNI 调用时运行良好的代码的一个常见问题是 VM 的类路径。虽然 java.exe
将解析 *
并将每个匹配的 JAR 添加到类路径,但使用调用接口(interface)的程序必须自己执行此操作。 JavaVMOption
中的 -Djava.class.path
仅适用于真实文件。此外,您只能使用实际的 VM 选项,而不能使用 -cp
之类的参数,因为它们也只能由 java.exe
解析,而不是调用接口(interface)的一部分。
关于Java native 接口(interface) - C++ 不等待 java 函数完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56729163/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!