| 7 | using namespace std; |
| 8 | |
| 9 | JNIEXPORT jstring |
| 10 | |
| 11 | JNICALL 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 | } |
nothing calls this directly
no test coverage detected