I'm working on a project recently, Need tojava Call insidepython Code, I found some information on the Internet, I would like to summarize briefly. But I am right.java Operation mechanism is not well understood, So we can only talk about how to do it.




1. installJython ( What is?Jython <https://baike.so.com/doc/5458872-5697261.html>)




Download and installjython_installer-2.5.0.jar
<http://download.csdn.net/download/radhot/1572368>,
After installation, thejython.jar Copy tolib in( Introducejar package);




2. Simple compilationpython Code





import java.io.IOException; import org.python.util.PythonInterpreter; public
class test { public static void main(String args[]) throws IOException {
PythonInterpreter interpreter = new PythonInterpreter(); // Functionpython Sentence
interpreter.exec("a = \"hello, Jython\""); interpreter.exec("print a"); } }
Result reported wrong:console: Failed to install '':
java.nio.charset.UnsupportedCharsetException: cp0.





Change the above code to the following form


import java.io.IOException; import org.python.util.PythonInterpreter; public
class test { public static void main(String args[]) throws IOException { <span
class="space"
style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit">
</span><span class="space"
style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit">
</span><span class="space"
style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit">
</span><span class="space"
style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit">
</span>Properties props = new Properties();
props.put("python.console.encoding", "UTF-8"); // Used to prevent: console:
Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
props.put("python.security.respectJavaAccessibility", "false"); //don't respect
java accessibility, so that we can access protected members on subclasses
props.put("python.import.site","false"); Properties preprops =
System.getProperties(); PythonInterpreter.initialize(preprops, props, args);
PythonInterpreter interpreter = new PythonInterpreter(); // Functionpython Sentence
interpreter.exec("a = \"hello, Jython\""); interpreter.exec("print a"); } }3.
implementpython file





Carry out the abovepython Change the code of the statement to


interpreter.execfile("C:/Users/JingYi/Desktop/hello.py"); /// implementpython py file
4. implementpython Document and transfer

package jp; import java.util.Properties; import org.python.core.Py; import
org.python.core.PyException; import org.python.core.PyObject; import
org.python.core.PyString; import org.python.util.PythonInterpreter; public
class main { public static void main(String args[]) { Properties props = new
Properties(); props.put("python.console.encoding", "UTF-8"); // Used to
prevent: console: Failed to install '':
java.nio.charset.UnsupportedCharsetException: cp0.
props.put("python.security.respectJavaAccessibility", "false"); //don't respect
java accessibility, so that we can access protected members on subclasses
props.put("python.import.site","false"); Properties preprops =
System.getProperties(); String[] arguments ={ "string1", "string2","string3"};
PythonInterpreter.initialize(preprops, props, arguments); PythonInterpreter
interpreter = new PythonInterpreter(); interpreter.exec("import sys");
interpreter.exec("sys.path.append('E:/Python27/Lib')"); // Path to load third party packages
interpreter.exec("sys.path.append('E:/Python27/Lib/site-packages')"); //
Path to load third party packages
interpreter.exec("sys.path.append('E:/Python27/Lib/site-packages/numpy/lib')");
// Path to load third party packages interpreter.execfile("C:/Users/JingYi/Desktop/hello.py");
// implementpython file String funcName ="myFunction"; PyObject someFunc
=interpreter.get(funcName); // callpython Monkey function of if (someFunc == null) { //throw
new Exception("Could notfind Python function: " + funcName); } else {
someFunc.__call__(new PyString(arguments[0]), new PyString(arguments[1]), new
PyString(arguments[2])); // Pass the parameters } }// main } amonghello.py The contents of the document are as follows:


def myFunction(str1, str2, str3): print 'print parameters from java' print
str1 print str2 print str3
5. Import third party Library





According to the above method, I don't know how tojython Similar to reference innumpy pandas Such a third-party library, Here is an implementation method( Andjython Irrelevant).
package jp; import java.io.BufferedReader; import java.io.InputStreamReader;
public class test { public static void main(String[] args){ try{
System.out.println("start"); Process pr =
Runtime.getRuntime().exec("E:\\Python27\\python.exe
C:/Users/JingYi/Desktop/hello.py"); // Nativepython Running environment & About to callpy file
BufferedReader in = new BufferedReader(new
InputStreamReader(pr.getInputStream())); String line; while ((line =
in.readLine()) != null) { System.out.println(line); } in.close(); pr.waitFor();
System.out.println("end"); } catch (Exception e){ e.printStackTrace(); } } }
Reference resources:


1.  Jython:java callpython The third party package path of files java callpython Total appearancepython Third party library not defined
<http://blog.csdn.net/ztf312/article/details/51338060>





2.  java callpython Total appearancepython Third party library not defined
<http://blog.csdn.net/johline/article/details/65633466>