Python代码:
import odbchelper
def add(x, y):
"""
add method
"""
return x+y
def sub(x, y):
"""
subtraction method
"""
return x-y
def invokeOdbchelper():
"""
invoking odbchelper.py buildConnectingString()
"""
myParams = {"server":"mpilgrim", \
"database":"master", \
"uid":"sa", \
"pwd":"secret"
}
return odbchelper.buildConnectionString(myParams)
if __name__ == "__main__":
print(add(10, 20))
print(sub(20, 19))
print(invokeOdbchelper())
java代码:
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class FirstJavaScript
{
public static void main(String args[])
{
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("E:\\_Python\\diveintopythonzh-cn-5.4b\\py_test\\pythonTest.py");
PyFunction func = (PyFunction)interpreter.get("add",PyFunction.class);
int a = 2010, b = 2 ;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());
}//main
}
运行后抛出错误:
Exception in thread "main" Traceback (most recent call last):
File "E:\_Python\diveintopythonzh-cn-5.4b\py_test\pythonTest.py", line 4, in <module>
import odbchelper
ImportError: No module named odbchelper
求解答!!!!! 为什么会抛出如此错误,单独运行Python是正常的。。
------解决方案--------------------
Process proc = Runtime.getRuntime().exec("python E:\_Python\diveintopythonzh-cn-5.4b\py_test\pythonTest.py");
proc.waitFor();