1

Can Python invoke the Java Framework?

I want to know whether a Python project can invoke a Java Framework, I find a Java Framework in GitHub, whether I can use it in my Python project?

qg_java_17137
  • 3,310
  • 10
  • 41
  • 84
  • 1
    In general, no. It might be possible by writing a C wrapper, but if you have to ask this question, that's probably not what you want or need. – Thomas Jan 15 '18 at 15:27
  • 1
    It *might* be possible by using Jython, though that might introduce incompatibilities to other (third-party) libraries you are using – UnholySheep Jan 15 '18 at 15:29
  • There is Jython which you can use for Python-Java Interop but you would lose all the C libraries... – etaloof Jan 15 '18 at 15:30
  • 1
    You could also try Py4J. https://www.py4j.org/index.html – Anthony Okoth Jan 15 '18 at 15:31

2 Answers2

1

Normally Python and Java have their own interpreters/VM's and cannot be shared. It is possible to use Jython but has limitations (fe. python version and support/compatibility with other python packages).

The interpreter and JVM do not match: Java has strict typing, python not. Java compiles and run, python is an interpreter and can change code in runtime (if you want). These are extra challenges why putting all in a same environment is very complex.

There are possibilities like a client/server architecture, but the feasability depends on the level of the framework.

Most of the time low level frameworks are optimized to run directly inside your application process. Any loose coupling will introduce performance and security and compatibility issues. Just think about how reflection will work or multiple inheritance.

If it is a high level framework (fe able to run stand alone) it is more feasable to use some sort of client/server. But still you have to develop a lot for it.

Industry standard is just to implement the framework of your desire in the language you want, then you can get also all the benefits of your platform.

J. Bakker
  • 336
  • 1
  • 6
1

Jython

Jython is one method of calling Java from python -- actually, you run your Python inside Java JVM. This gives you access to almost any Java that runs on JVM, but comes with many limitations.

Because Jython is running python inside the JVM, this gives you acess to almost any Java library. However, you are very restricted in what Python you can use: you can only use Python 2.7, and can import pure Python libraries only (compiled Python libraries with C will not run on Jython).

For an example of a project that uses Jython: Processing.py runs on Jython in order to access the Processing Java API and its ecosystem of Java libraries.

Note that Jython 2 and its docs are quite old, and that the developers are uncertain if / when Jython 3 will be released.

py4j

py4j is a different approach -- it is "A Bridge between Python and Java" and lets native python code access separate Java running in a separate JVM. Note however that the python and Java code must be running in parallel and communicating through a gateway interface. This is communication between separately running processes -- you are not spinning up a JVM from Python or inside Python.

For example: on the JVM side pass myObject to a new GatewayServer(myObject); on the Python side create a JavaGateway() Python object and use it to communicate with the Java myObject.

JeremyDouglass
  • 1,361
  • 2
  • 18
  • 31
  • Whats the meaning of `communicating through the Gateway` in `py4j`? – qg_java_17137 Jan 16 '18 at 02:17
  • See the simple example at the top of the py4j homepage. On the JVM side you pass an object to a new GatewayServer(object). On the Python side, create a JavaGateway() python object and use it to communicate with the Java object. – JeremyDouglass Jan 16 '18 at 02:37