Since you have not mentioned how robust your application is I can think of a solution which can be used if you are planning for a higher level architecture.
- Create a python based web application (HTTP server) with all logic to process your files.
- Create a java app which can communicate via HTTP python server to get the CSV processed information.
Try to avoid Runtime execution of commands with in your codes that faces user as if it not is properly managed there is always a chance for security breach
Answer from Santosh Balaji Selvaraj on Stack OverflowSince you have not mentioned how robust your application is I can think of a solution which can be used if you are planning for a higher level architecture.
- Create a python based web application (HTTP server) with all logic to process your files.
- Create a java app which can communicate via HTTP python server to get the CSV processed information.
Try to avoid Runtime execution of commands with in your codes that faces user as if it not is properly managed there is always a chance for security breach
There are more ways to do that:
Solution 1:
Use the python library from java with System.loadLibrary, and call the python method. (here is an example using C/C++: Calling a python method from C/C++, and extracting its return value)
Solution 2:
Launch python as another process, and use D-Bus (or something similar) to communicate with it.
I attempted to code a solution using pipes but it seems that they just aren't well suited to sending multiple messages back and forth with potentially large data attached. Rather, they seem ideal for opening a "worker" style program that runs, responds, and dies.
Looking into socket programming, I found a fantastic resource here: https://web.archive.org/web/20080913064702/http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
The tutorial presents TCP and UDP variants of a simple chat program written in 4 languages. I ended up using and modifying the TCP Java client and Python server.
This is the opensource solution Google uses to do IPC between Java and Python. https://code.google.com/p/protobuf/
Recommended.
How can I transfer data between Java and Python
What kind of data? And what operating system?
Here is some general reading: https://en.wikipedia.org/wiki/Inter-process_communication
More on reddit.comCommunication between python client and java server - Stack Overflow
Communicate with Java program from Python - Stack Overflow
Both ways, java <=> python, communication using py4j - Stack Overflow
Videos
Hello,
I'm concurrently running two separate programs, one in Java and another in Python. I need to get some data from the Java program into the Python one, and I'm not sure how I could do it. My current application wouldn't allow me to use Jython, so other solutions would be pretty cool.
Edit : It's some numerical Data, in Linux
What kind of data? And what operating system?
Here is some general reading: https://en.wikipedia.org/wiki/Inter-process_communication
You can check this out:
https://en.wikipedia.org/wiki/Inter-process_communication
If you use jython then you can probably make method calls between the two codebases.
Append \n to the end of data:
client_socket.send(data + '\n')
ya..you need to add '\n' at the end of the string in python client..... here's an example... PythonTCPCLient.py
`
#!/usr/bin/env python
import socket
HOST = "localhost"
PORT = 8080
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
sock.sendall("Hello\n")
data = sock.recv(1024)
print "1)", data
if ( data == "olleH\n" ):
sock.sendall("Bye\n")
data = sock.recv(1024)
print "2)", data
if (data == "eyB}\n"):
sock.close()
print "Socket closed"
`
Now Here's the java Code: JavaServer.java `
import java.io.*;
import java.net.*;
class JavaServer {
public static void main(String args[]) throws Exception {
String fromClient;
String toClient;
ServerSocket server = new ServerSocket(8080);
System.out.println("wait for connection on port 8080");
boolean run = true;
while(run) {
Socket client = server.accept();
System.out.println("got connection on port 8080");
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
fromClient = in.readLine();
System.out.println("received: " + fromClient);
if(fromClient.equals("Hello")) {
toClient = "olleH";
System.out.println("send olleH");
out.println(toClient);
fromClient = in.readLine();
System.out.println("received: " + fromClient);
if(fromClient.equals("Bye")) {
toClient = "eyB";
System.out.println("send eyB");
out.println(toClient);
client.close();
run = false;
System.out.println("socket closed");
}
}
}
System.exit(0);
}
}
` Reference:Python TCP Client & Java TCP Server
My project uses some packages that are available only in Python and heavily rely on C libraries. The project also greatly benefits from Java libraries and the JVM. What's the optimal way to call Python functions from Java?
I tried:
-
Small web-services: overhead to serialize data, start and stop the services. Also debugging is harder and implementing each new function is now double the effort.
-
Jpy: a library that runs an interpreter in the JVM. Spare the service start/stop, but: isn't really feasible for more than a single-liner, data translation between Java and Python is cumbersome, and I also encountered runtime segmentation fault errors.
Any other options?
The project is in the machine-learning domain, so involves exchanging large numeric arrays and text. In some cases the execution switches back and forth between the platforms.