Question

Is it possible to compile a library intended for Java with GCJ, get a dll and call from python ctypes?

I'm interested in toxilibs for now, but if anybody knows a toy example that would be great !

Was it helpful?

Solution

If you want Java-Python hooks, you'd be far better off using Jython and then calling across the boundary that way.

However, yes, it's possible to call an external library from Java; but you don't need GCJ to do that. Rather, you can just bring up a JVM instance inside your Python runtime and then invoke your method(s) for that.

JNI invocation spec

Basically, you want to create your VM at startup, then invoke your method(s) whenever you want:

// Do this once per session, e.g. an __init__ 

JNI_CreateJavaVM(&jvm, &env, &vm_args); 

// When needed invoke Example.foo(int)
jclass cls =
env->FindClass("Example");  jmethodID
mid = env->GetStaticMethodID(cls,
"foo", "(I)V"); 
env->CallStaticVoidMethod(cls, mid,100);

You could write some simple C-wrapper code to invoke this for you from ctypes. However, the JavaVM is a structure of a structure with a number of void* pointers, so might ne non-trivial to do it directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top