1

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.

yuez
  • 783
  • 1
  • 7
  • 16
  • You could literally run a jar file from python, passing the query as a parameter, and then read it's standard output. I posted this as a comment and not an answer becase I don't know the specifics of this – Cruncher Aug 30 '13 at 17:17
  • http://stackoverflow.com/questions/89228/calling-an-external-command-in-python this should help. If you know java command line syntax it shouldn't be too difficult – Cruncher Aug 30 '13 at 17:20

3 Answers3

0

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.

duffymo
  • 293,097
  • 41
  • 348
  • 541
0

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.

a p
  • 2,785
  • 2
  • 22
  • 40
0
  1. 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.

  2. 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.

  3. There is a lot packages at pypi, like JCC (from PyLucene creator) or Py4J that can use Oracle JavaVM through JNI or sockets.

beatt
  • 96
  • 1
  • 7
  • For Excelsior Jet: they provide usual JNI calls to their JVM; but JNI is very inexpensive (in compare with Oracle JVM JNI calls) – beatt Feb 17 '14 at 12:52