Python Integration with Java / C - java

I implemented most of my projects in C++ and python. However, we recently got a new database interface that I could only use Java to retrieve data.
I want to stay with my Python/C++ tools but I am wondering if there is a good solution to integrate Java to my Python application. I heard about Jython, but it is a different python implementation and I am concerned some of my C++ tools will not work well with it. Jpype seems simple but it hasn't been updated since 2011, so a little concerned with the compatiablity with the current python/java.
Is there a good solution to this? all opinions are welcomed.

gcj (gcc compiler for java) supports java 1.5 syntax (1.4 is working better on it) and therefore some Java programs may be compiled to native code. gcjh (or javah) can produce headers for java libraries, so you can write C extensions for python. Of course some libraries could not be compiled with gcj (like Apache Commons Logging) because of using com.sun packages. Did not updated from 2009.
There is another Java to native compiler, commercial Excelsior Jet (it's another JavaVM, it supports Java 1.6 and soon Java 1.7). They said linux-64bit version of their product will be available in 2013-Q4. But I didn't try it well, I don't know, are headers for compiled library can be produced.
There is a lot packages at pypi, like JCC (from PyLucene creator) or Py4J that can use Oracle JavaVM through JNI or sockets.

One way to do this is to write web services. A web service can accept an HTTP request, marshal it into a data request, pass that to a Java class that get the data out, map the quert results into a response of some kind, and send it back.
Any client that can send an HTTP request, accept the response and unmarshal it can interact with that service. They need not know that it's implemented in Java.
You pay the price of an extra network roundtrip to get the benefit of language interoperability.

There are ways of wrapping C/C++ out there, but I can't speak to them.
Integrating with Java, however, is wonderfully simple. There's a distribution of Python called Jython which actually runs on the JVM. Using Java libraries is intuitive and easy:
from java.io.util import *
and for the most part it just works (some caveats apply in terms of threaded/async stuff). I love Jython. We used Jpype for a few projects but even if it were still fully maintained I'd still choose Jython over it any day for this sort of project.

Related

Using GraalPython as a Jython replacement

I wonder if it is possible to use GraalPython as a Java library to interpret Python code on a standard JVM. If so, would it be a good choice to replace Jython (which only supports Python 2.7)?
Is this possible without importing the entire GraalVM project? I expect only Truffle and the Python interpreter built on top of it should be necessary.
If this is not possible, are there any good Java implementations of Python 3 available?
You should be able to run any GraalVM language on any JDK as their are just Java programs. However, the performance will be affected a lot. Moreover, languages like python consist of additional resources (standard library files, etc.) that you would have pull from GraalVM too.
This document about GraalVM JavaScript discusses this in more detail and describes how to run GraalVM JavaScript on stock JDK without compromising the performance. Some of it can be applicable to GraalPython.
https://github.com/graalvm/graaljs/blob/master/docs/user/RunOnJDK.md
Tl;dr: it will be much easier to use GraalVM. It's full JDK distribution. You are not missing on anything. If you can't, there are some ways.

Can Python invoke the Java Framework?

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?
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.
https://github.com/jdf/processing.py
Note that Jython 2 and its docs are quite old, and that the developers are uncertain if / when Jython 3 will be released.
https://github.com/jython/jython3
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.
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.

Java Projects on Django?

ok, So i searched net for the possible implementation but all that I managed to find is Django projects implementation on Java platform through Jython. But I want to do the reverse, i.e. implement/integrate java project ( which in my case is SAIKU server ) on Django platform.
The question being, is it possible, and if yes, then kindly point me to the solution.
Thanking in advance =)
For your specific requirement, I would suggest using RESTFul API to access the Saiku Server.
However if you need to run Java Classes from Python.
Here are the options available for you:
JCC -- a C++ code generator for calling Java from C++/Python. It produces producing Python extensions which communicate via JNI with a Java virtual machine. As it implies, this would require compilations of every possible call. However this project is backbone of PyLucene project.
CodeMesh. C++ code generator for Java.
Py4J Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine.
JPype allow python programs full access to java class libraries. It is done through interfacing at the native level in both Virtual Machines. However there are no recent development in this front.
In general, having an loosly coupled integration through REST or RCP would be easy to maintain than tightly coupled JNI based implementation.
There's no way to run Java within the Python runtime (which is what it sounds like you want). There are Java to Python "translators" available, but they're terrible. Honestly, if you need a Java server and Django to sit inside the same process for some reason, Jython is the way to go.
There are lots of options outside of that though, off the top of my head:
Implement Python bindings for your server (See PyLucene for an example)
Implement a socket server within your Java server that Python can talk to directly

Use python library in java code

There is some library called pymorphy written in python. Unfortunately, for java there is not any library with the similar functionality - natural language processing for Russian lang. So I need to invoke some methods of pymorphy library from Java code.
First I've tried to solve this problem with Jython. But I've spent 2 days and the goal was not accomplished because python modules cdb, bsddb3, sqlite are written in C and they will not work with Jython.
Now I want to run some python light-weight server with pymorphy for handling request from Java code.
How could I implement this kind of java-python interaction with the maximum production performance? Or is there more simple way to call python from java?
Try Jepp, "Java Embedded Python". http://jepp.sourceforge.net/
I haven't used it beyond small projects, but it works as advertised, allowing one to call CPython transparently from Java. If you have the opposite problem, needing to call Java from CPython, definitely check out JPype. I've used it extensively and it works very well.
I think these libraries (cdb, bsddb3, sqlite) has a jython implementation in https://code.google.com/p/django-jython/ check it out

How can Java code be quickly converted to .NET (specifically, ASP.NET)?

Are there any tools available to simplify the conversion?
What percentage of Java code is converted exactly to .NET code by these tools?
You can try IKVM. Saxon, an extremely popular Java XQuery/XSLT engine by Michael Kay, uses IKVM.
IKVM.NET is an implementation of Java
for Mono and the Microsoft .NET
Framework. It includes the following
components:
* A Java Virtual Machine implemented in .NET
* A .NET implementation of the Java class libraries
* Tools that enable Java and .NET interoperability
http://www.ikvm.net/
You could try this one
http://mayaf.org/janett/
Janett translates Java syntax, constructs and calls to Java libraries to C# / .Net counterparts.
Java is roughly equivalent to c#, so translating to c# should be relatively straightforward.
The path from a Java webserver to ASP.NET is less clear.
You might try to the Java Language Conversion Assistant from Microsoft. I think they may have dropped support, but now ArtInSoft seems to have a related product:
http://www.artinsoft.com/pr_jlca.aspx
There's also Octopus from Remotesoft:
http://www.remotesoft.com/octopus/
The issue with porting from Java is usually not so much the language itself, which is pretty similar to C#, as it is the libraries.
Of course, a web app will present an additional layer of complexity. If the app doesn't have too much code at the page rendering phase, it shouldn't be very difficult. If the page is riddled with code, then you may be better off re-writing it than trying to port directly.
Have you considered using J#? It's no longer supported but if you're just trying to get onto a .NET language, that might be the fastest path.

Categories