Hi,
I am trying to interface between Java and Python in a program called Eclipse. Any insight on how to do this would be helpful. Thanks!
Elise
As asked, the first decision is what "interface" means here: does Java need to call Python, does Python need to call Java, must the integration be in-process (same JVM) or can it be out-of-process, and are CPython C-extensions (numpy, scipy, etc.) required? Answering those questions narrows the right tool quickly.
As suggested, Jython is the simplest when an in-JVM, in-process Python runtime is acceptable and the code is pure Python (no C extensions). It lets Java and Python objects interoperate directly and is easy to add to an Eclipse Java project. Caveat: Jython historically lacked CPython C-extension support and Python-3 parity, so it is not a fit if the project depends on native Python modules.
If CPython compatibility or native extensions are required, consider:
A minimal Java pattern for running an external Python script (avoid deadlocks by consuming streams):
Process p = new ProcessBuilder("python", "script.py", "arg1")
.redirectErrorStream(true)
.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = r.readLine()) != null) System.out.println(line);
p.waitFor(); Quick troubleshooting checklist:
Jump to Post— HarryPad 0OK, I think you could share a little more information.
Now what is it that you are trying to accomplish? (in English).
And what is it that your are trying to interface?
Do you want to call python from Java (if so - why?)
Do you …
OK, I think you could share a little more information.
Now what is it that you are trying to accomplish? (in English).
And what is it that your are trying to interface?
Do you want to call python from Java (if so - why?)
Do you want to call Java from python (if so - why?)
And have you looked at (ie: google for it) Jython - a python implemented in java that has hooks so that it can be called from Java?
Anyway - we are all glad to help here but you really do need to share more information so that we can steer you in the best way.
Harry
Definitely Jython. It's Python that is implemented on the Java platform.
Look into it at http://www.jython.org/.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.