1

I need to call some Python code from Java.

I am aware though that Jython exists or that Java has its own Python interpreter.

Thing is, said Python code uses some native (C) compiled code, even runs something on GPU.

So I can divide this into two questions:

  1. Does Jython or Java's Python interpreter support this? That is, the Python code (when called from Java) would run with all the C and GPU parts, thus will run roughly as fast as it would on its own.
  2. If neither of the above mentioned ways support this, is there any other way to do this?

P.S. If that helps, although I suppose it's irrelevant, the code I need to run are highly GPU/C optimized neural networks from Keras (with TensorFlow underneath). I absolutely can't afford to run the only-Python interpreted version.

4
  • Is the java code compiled? Are you able to issue system commands? You can, from python, run the java class command line if so. If you aren't aware of how that works, look up os.system("command")
    – Davy M
    Commented Dec 7, 2017 at 23:00
  • I need to run Python from Java, not other way around.
    – awa993
    Commented Dec 7, 2017 at 23:04
  • Whoops, I read that backwards, sorry about that. Though my suggestions still stands, in java there are system commands as well that you could use to run python.
    – Davy M
    Commented Dec 7, 2017 at 23:06
  • IIRC Jython does not support libraries that use Cython. I would suggest using stdin/stdout, TCP, or Remote Procedure Call, and run the two codes in two processes. Alternatively you could write C/C++ codes and use JNI + Python C-API to bridge them together.
    – Dabiuteef
    Commented Dec 8, 2017 at 4:56

1 Answer 1

1

Currently jython does not support native compiled python modules. In order to run native modules you will need access to a native python(cpython) interpreter from java. There are several open source projects that use JNI to access a cpython interpreter. Three projects that you can look into are JEP, JPY, and JyNI. In regards to GPU access, I only have experience with JEP which I have used with PyCUDA to execute code on the GPU. While I don't have personal experience with tensorflow, I know there are posts on the JEP mailing list regarding using JEP and tensorflow so I believe there are other projects using this combination successfully.

3
  • Alright, I will look into it, if I need. Currently I am working on another solution. Thanks for the answer.
    – awa993
    Commented Dec 20, 2017 at 18:46
  • @awa993 did you end up using JEP ? was the experience successful ? Commented Apr 25, 2018 at 11:46
  • @AlessandroBenedetti No, I didn't. I ditched Java entirely for this project.
    – awa993
    Commented Apr 25, 2018 at 20:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.