java does not display the contents of the running bug python runtime by calling python

Let me talk about the reasons under the above problem, the above problem is because python used in third-party libraries, the library is not on your computer, so the program is not running, will see output in the console. Once you import the library just fine. . .

python import library, the library can be downloaded separately installed, you can also directly download  Anaconda  (It contains the python numpy pandas) but relatively large.

 But I was not with the above two methods, I was opened in pycharm the python program, pycharm prompted to do not automatically download library (similar to the ideal write java code automatically lead pack), I chose to import. (Brute)

pycharm installation: https://blog.csdn.net/asd343442/article/details/84103444

  

Recently the company to use java call python.

 Call python java have learned through Baidu Runtime () and Jython in two ways. runtime for simple procedures can be used directly, but also what not lead pack.

  Jython is implemented in python java language platform. We usually use python actually Cython python is implemented in C language. Just python language specification, implementation needs to languages ​​such as C JAVA execution. 

 Jython use, requires the installation environment and introducing Jython Jython leader packet.

 Reference article: https://www.cnblogs.com/nuccch/p/8435693.html

                 https://blog.csdn.net/it_xiao_bai/article/details/79074988

runtime java python call reference code:

public class CallPy {
    public static void main(String[] args) throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        int month = 5;
        Process proc;
        String lastLine = null;
        try {
            String[] strings = {"python", "E:\\devTool\\Pyproject\\demo\\second.py"};
            String [] Strings2 = { "E: DevTools \\ \\ \\ python.exe Python", "D: \\ add.py" };
             // execute py file \\ 
            proc = Runtime.getRuntime () Exec (. strings);
             // with the results of input and output streams to intercept 
            the BufferedReader in = new new the BufferedReader ( new new the InputStreamReader (proc.getInputStream ()));
            String content = null;while (true){
                lastLine = content;
                if ((content = in.readLine()) == null){
                    System.out.println(lastLine);
                    break;
                }
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace ();
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
String substring = lastLine.substring(1, lastLine.length() - 1);
String[] split = substring.split(" ");

    


 The first pit: the above code, the last line in the yellow part is taken out of the python output.

Indigo upper portion, the upper portion is written in the address 2 python run file, the following refers to the output character string is cut to give space array. Because there are multiple operating environment python on my computer (himself installed a python, installed jython), if you do not specify python.exe absolute path, in accordance with the following string array solution obtained by cutting the space in the middle will be mixed with air string, the solution is to split ( "") quotation marks enter two spaces.

The second pit: a computer environment without the python, python error will be reported can not load the file. .

Guess you like

Origin www.cnblogs.com/prader6/p/11016802.html