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




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




Download and install jython_installer-2.5.0.jar
<http://download.csdn.net/download/radhot/1572368>,
After installation, the jython.jar Copy to lib in ( introduce jar package );




2. Easy to write python 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(); // function python sentence
interpreter.exec("a = \"hello, Jython\""); interpreter.exec("print a"); } }
Result reporting error :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(); // function python sentence
interpreter.exec("a = \"hello, Jython\""); interpreter.exec("print a"); } }3.
implement python file





Carry out the above python Change the code of the statement to


interpreter.execfile("C:/Users/JingYi/Desktop/hello.py"); /// implement python py file
4. implement python 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");
// implement python file String funcName ="myFunction"; PyObject someFunc
=interpreter.get(funcName); // call python 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 } among hello.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 to jython Similar to reference in numpy pandas Such a third-party library , Here is an implementation method ( And jython 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"); // Native python Operating environment & About to call py 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 call python The third party package path of files  java call python Always present python Third party library not defined
<http://blog.csdn.net/ztf312/article/details/51338060>





2.  java call python Always present python Third party library not defined
<http://blog.csdn.net/johline/article/details/65633466>