Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke Python modules from Java

Tags:

java

python

ipc

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.


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.

like image 630
user36813 Avatar asked Jan 17 '09 22:01

user36813


People also ask

Can we invoke Python code from Java?

You can easily call python functions from Java code with Jython. That is as long as your python code itself runs under jython, i.e. doesn't use some c-extensions that aren't supported. If that works for you, it's certainly the simplest solution you can get.

How do you trigger a Python script in Java?

You can use Java Runtime. exec() to run python script, As an example first create a python script file using shebang and then set it executable.

Can Java interact with Python?

The seamless interaction between Python and Java allows developers to freely mix the two languages both during development and in shipping products.


2 Answers

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.

like image 114
Igal Serban Avatar answered Sep 18 '22 22:09

Igal Serban


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.

like image 34
Tamás Avatar answered Sep 21 '22 22:09

Tamás