tags:

views:

723

answers:

3

I have a Python interface of a graph library written in C - igraph (the name of library). My need is to invoke the python modules pertaining to this graph library from Java code. It goes like this, the core of library is in c. This core has been imported into Python and interfaces to the functions embedded in core are available in Python. My project's rest of the code is in Java and hence I would like to call the graph functions by Java as well. Jython - which lets you invoke python modules with in Java was an option.I went on trying Jython to discover that it will not work in my case as the core code is in C and Jython wont support anything that is imported as a c dll in python code.I also thought of opting for the approach of calling graph routines directly in c. That is without passing through Python code. I am assuming there must be something which lets you call c code from Java, how ever I am not good in C hence I did not go for it. My last resort seems to execute Python interpreter from command line using Java. But that is a dirty and shameless. Also to deal with the results produced by Python code I will have to write the results in a file and read it back in java. Again dirty way. Is there something that any one can suggest me? Thanks to every one giving time.

+2  A: 

Never tried it. But I recently stumbled on a project named Jepp that may be of interest to you.

Jepp embeds CPython in Java. It is safe to use in a heavily threaded environment, it is quite fast and its stability is a main feature and goal.

Igal Serban
A: 

Thanks Igal for answering. I had a look at it. At first glance it appears as if it is simply calling the python script.

Jep jep = new Jep(false, SCRIPT_PATH, cl);
jep.set("query", query);
jep.runScript(SCRIPT_PATH + file);
jep.close();

Isnt it very similar to what we would do if called the python interpreter from command line through a Java code.

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("python test.py");

Concern is how do I use the results generated by Python script. The naive way is to write them to file and read it back in Java. I am searching for a smarter approach.Thanks for suggestion anyway.

Keep reading on that page. In particular: "This sets a Java object in the python jep module created at runtime. ... If the script alters the query object, the caller's query will also be changed. Both the script and the caller refer to the same object instance.
Dave Ray
A: 

If you want to call C functions from Java, JNA (Java Native Access) is probably the way to go. JNA allows you to call functions in native libraries without having to write the C glue code (as you would have to when using JNI), and automatically maps between primitive data types in Java and C. A simple example might look like this:

import com.sun.jna.Native;
import com.sun.jna.Library;

public class PrintfWrapper {
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary)Native.loadLibrary("c", CLibrary.class);
        void printf(String formatString, Object... args);
    }

    public static void main(String[] args) {
        CLibrary.INSTANCE.printf("Hello, world\n");
    }
}

However, things will get complicated with igraph because igraph uses many data structures that cannot be mapped directly into their Java counterparts. There is a project called JNAerator which should be able to generate the JNA source from igraph's header files, but I have never tried it and chances are that the results will still need some manual tweaking.

Also note that a Java interface for igraph is being developed slowly but steadily and it might become useful in a few months or so.

Tamás