Interaction between Java App and Python App

自作多情 提交于 2019-12-01 23:49:25

Look into Jython - you can run Python programs directly from Java code, and interact seamlessly back and forth.

I don't know nothing about Jython and the like. I guess it's the best solution if you can execute two programs without executing a new process each time the Java app needs to transform text. Anyway a simple proof of concept is to execute a separate process from the Java App to make it work. Next you can enhance the execution with all that tools.

Executing a separate process from Java

String[] envprops = new String[] {"PROP1=VAL1", "PROP2=VAL2" };
Process pythonProc = Runtime.getRuntime().exec(
   "the command to execute the python app", 
    envprops, 
    new File("/workingdirectory"));

// get an outputstream to write into the standard input of python
OutputStream toPython = pythonProc.getOutputStream();

// get an inputstream to read from the standard output of python
InputStream fromPython = pythonProc.getInputStream();

// send something
toPython.write(.....);
// receive something
fromPython.read(....);

Important: chars are NOT bytes

A lot of people understimate this.

Be careful with char to byte conversions (remember Writers/Readers are for chars, Input/OutputStreams are for bytes, encoding is necesary for convertir one to another, you can use OuputStreamWriter to convert string to bytes and send, InputStreamReader to convert bytes to chars and read them).

Use ProcessBuilder to execute your Python code as a filter:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PBTest {

    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("python", "-c", "print 42");
        pb.redirectErrorStream(true);
        try {
            Process p = pb.start();
            String s;
            BufferedReader stdout = new BufferedReader (
                new InputStreamReader(p.getInputStream()));
            while ((s = stdout.readLine()) != null) {
                System.out.println(s);
            }
            System.out.println("Exit value: " + p.waitFor());
            p.getInputStream().close();
            p.getOutputStream().close();
            p.getErrorStream().close();
         } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Expose one of the two as a service of some kind, web service maybe. Another option is to port the python code to Jython

One possible solution is jpype. This allows you to launch a JVM from Python and pass data back and forth between them.

Another solution may be to write the Python program as a filter (reading data from stdin and writing result to stdout) then run it as a pipe. However I do not know how well Java supports this - according to the Sun docs their concept of pipes only supports communication between threads on the same JVM.

An option is making the python application work as a server, listens for request via sockets (TCP).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!