Invoke Python modules from Java

后端 未结 3 1595
心在旅途
心在旅途 2020-12-31 21:55

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

3条回答
  •  感动是毒
    2020-12-31 22:37

    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.

提交回复
热议问题