2016-10-27 22 views
0

我想连接一个c方法与python脚本。我做到了用:连接C方法与python脚本

#include<stdio.h> 
void pythonEx(){ 
system("pythonScript.py"); 
} 

这样我可以运行Python脚本,但我不知道如何从方法到python脚本传递数据。

+1

通过数据如何?使用命令行? –

+1

难道你不能把数据作为参数传递给python脚本吗? –

+0

本链接详细介绍了在C程序中使用python的更好方法: http://www.linuxjournal.com/article/8497 –

回答

1

我建议在这里通过JNI去。

首先,你需要JNI代码,将调用Python的lib:

#include "jni.h" 
#include "runscript27.h" 
#include "python/Python.h" 
#include <stdio.h> 

static PyThreadState *tstate = NULL; 
static PyGILState_STATE gstate = 0; 

// Initialization should be done once 
JNIEXPORT void JNICALL Java_python_Python_pyinitialize 
    (JNIEnv *env, jclass obj) { 

    Py_Initialize(); 

    // This is important!! 
    // Remember to reserve PyGILState 
    // It is not thread safe 
    gstate = PyGILState_Ensure(); 
} 

// Finalization should be done once 
JNIEXPORT void JNICALL Java_python_Python_pyfinalize 
    (JNIEnv *env, jclass obj) { 

    // once finished remember to release GIL 
    PyGILState_Release(gstate); 

    Py_Finalize(); 

} 

JNIEXPORT void JNICALL Java_python_Python_runstring27 
    (JNIEnv *env, jclass obj, jstring str) { 

    // we have to get string bytes into C string 
    const char *c_str; 
    c_str = (*env)->GetStringUTFChars(env, str, NULL); 
    if(c_str == NULL) { 
    return; 
    } 

    // Some info for the user 
    printf("Script from python.Python: %s\n", c_str); 

    PyRun_SimpleString(c_str); 

    // after using it, remember to release the memory 
    (*env)->ReleaseStringUTFChars(env, str, c_str); 
} 

JNIEXPORT void JNICALL Java_python_Python_runscript27 
    (JNIEnv *env, jclass obj, jstring str, jstring name) { 

    // we have to get string bytes into C string 
    const char *c_str; 
    c_str = (*env)->GetStringUTFChars(env, str, NULL); 
    if(c_str == NULL) { 
    return; 
    } 

    // we have to get string bytes into C string 
    const char *c_name; 
    c_name = (*env)->GetStringUTFChars(env, name, NULL); 
    if(c_name == NULL) { 
    return; 
    } 

    // Some info for the user 
    printf("Script from python.Python: %s\n", c_name); 
    printf("File with script: %s\n",c_str); 

    // We will open file passed as argument 
    // and call this script via Python library 
    FILE* file; 
    file = fopen(c_str,"r"); 

    PyRun_SimpleFile(file, c_str); 

    // close file, clean up, and return. That's it. 
    fclose(file); 

    // after using it, remember to release the memory 
    (*env)->ReleaseStringUTFChars(env, str, c_str); 
    (*env)->ReleaseStringUTFChars(env, str, c_name); 
} 

然后,您可以从Java调用此代码:

package python; 

import java.io.*; 

public class Python { 
    static { 
     System.loadLibrary("callpy27"); 
     Python.pyinitialize(); 
    } 

    public static native void runscript27(String script, String name); 
    public static native void runstring27(String script); 
    public static native void pyinitialize(); 
    public static native void pyfinalize(); 

    public static void main(String [] arg) { 
     // Super simple sctipt to call 
     String script = "print 'Hello from python'"; 

     // run script from file 
     try { 
      PrintWriter writer = new PrintWriter("script.py", "UTF-8"); 
      writer.println(script); 
      writer.flush(); 
      writer.close(); 

      pyinitialize(); 
      runscript27("script.py", "test"); 
      pyfinalize(); 
     } catch(Exception ex) { 
      ex.printStackTrace(); 
     } 

     // run script passed as string 
     try { 
      pyinitialize(); 
      runstring27(script); 
      pyfinalize(); 
     } catch(Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

对于全样本(带的Makefile和示例代码,看看这里:https://github.com/mkopsnc/keplerhacks/tree/master/python

+0

不好的答案表单。答案应该用多于一个简单的链接来解决问题,如果他们指向的网站不存在,则链接可以随时变得不可用。至少包含解决问题的代码片段。 – ryyker

+0

您发布的信息没有任何问题,它非常有用,并且只是以非常临时的形式提出问题。如果你真的不在意分数,这可能更适合作为评论。 – ryyker

+0

我不太清楚从Java调用Python的例子与如何从C中调用它的问题有关。然而,Java示例之前的答案部分似乎对OP很有用。 –