the python code is doing one function and the java code is doing one function, I want to integrate the both, or use my python code to call java code and output the result of that java code. What is the best way to do it?
2 Answers
Generally inter-language processing is typically pretty messy because both languages have their own runtimes that do memory management and memory layout differently. Since both languages have their own runtimes both runtimes have to run at the same time, and you have to have a way of integrating them.
There are many ways to do integrate them, and it heavily depends on your use case which one is better. One simple way to do this is to encapsulate your java function into a java command line program, which python can call as a remote process then parse the result. This solution is fairly inefficient, so it will be very slow. An article demonstrating that can be found here. http://fastml.com/how-to-run-external-programs-from-python-and-capture-their-output/
Other than that there are many methods of interprocess communication where you start both the java and python program, and have the python program make calls against the java program to get work done.
Essentially what you are asking for can be done, but you must consider the work and performance impact of doing so. You should always try to write your programs in one language.

- 406
- 3
- 6
You can use jython to run Python from Java, therefore run Python code that will utilize your Java code as well.
You can compile Java code into a DLL or shared object library and use it from Python through ctypes module.
You can use pipes or some other model of inter-process communications. With pipes is easy, just use subprocess to call Java interpreter that will run your Java code and output the result to a stdout, which you will read from Python.
Perhaps there are some more options, but I cannot think of any other at the moment.
As for which is the best way, well, it depends on your needs. How much speed do you need, how much data will you interchange, how easy your solution has to be, etc. etc.
Some more info on your part, about what are you trying to do, will help us to help you more.

- 4,128
- 1
- 17
- 35