0

I need to find a way of communication between java and python program. My application is Java, and another application ( python) will request me some data and trigger some process in my Java Application.

EDITED: My application is Java desktop application, that uses Jboss Application server.

For first release, I do not have enough time to make a comprehensive way of communication. So I am planing to use subprocess.Popen for first release. I will provide them a jar. Then they can call me from pyhton.

Actually I was planned to make a single class that takes some arguments on main. Then according to parameters, my application can determine to call related function. But there is a problem here. When they want to call my functions in following way. in each call, a new java process will be created and I can not keep some static variables from my application. Actually What I need is to run my application once, then access some functions from existing process.

#!/usr/bin/env python
from subprocess import Popen, PIPE

p = Popen(['java', '-jar', 'myjarfile.jar'], stdin=PIPE, stdout=PIPE)

Do you think Can I implement this using subprocess.Popen. If not can you show me an easy way ?

user725455
  • 465
  • 10
  • 36

1 Answers1

0

I would suggest using xmlrpc -- it's pretty simple:

import org.apache.xmlrpc.*;

public class JavaServer { 

  public Integer sum(int x, int y){
    return new Integer(x+y);
  }

  public static void main (String [] args){

    try {

      System.out.println("Attempting to start XML-RPC Server...");

      WebServer server = new WebServer(8080);
      server.addHandler("sample", new JavaServer());
      server.start();

      System.out.println("Started successfully.");
      System.out.println("Accepting requests. (Halt program to stop.)");

    } catch (Exception exception){
      System.err.println("JavaServer: " + exception);
    }
  }
}

(Source http://www.tutorialspoint.com/xml-rpc/xml_rpc_examples.htm)

Here's some code for a python client:

import xmlrpc.client
proxy = xmlrpc.client.ServerProxy("http://localhost:8080/")
today = proxy.today()

(Source: https://docs.python.org/3/library/xmlrpc.client.html)

All you'd have to do is make your methods and stitch them together.

kevmus
  • 1
  • 1