MCPcopy Create free account
hub / github.com/Firebasky/Java / jstring

Function jstring

JNI/jni/org_javaweb_jni_CommandExecution.cpp:9–28  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7using namespace std;
8
9JNIEXPORT jstring
10
11JNICALL Java_org_javaweb_jni_CommandExecution_exec
12 (JNIEnv *env, jclass jclass, jstring str) {
13 if (str != NULL) {
14 jboolean jsCopy;
15 const char *cmd = env->GetStringUTFChars(str, &jsCopy);// 将jstring参数转成char指针
16 FILE *fd = popen(cmd, "r");// 使用popen函数执行系统命令
17 if (fd != NULL) {
18 string result; // 返回结果字符串
19 char buf[128];// 定义字符串数组
20 while (fgets(buf, sizeof(buf), fd) != NULL) { // 读取popen函数的执行结果
21 result +=buf; // 拼接读取到的结果到result
22 }
23 pclose(fd);// 关闭popen
24 return env->NewStringUTF(result.c_str());// 返回命令执行结果给Java
25 }
26 }
27 return NULL;
28}

Callers

nothing calls this directly

Calls 2

GetStringUTFCharsMethod · 0.45
NewStringUTFMethod · 0.45

Tested by

no test coverage detected