Is the following usable with JNI?
public NativeClass {
static {
System.loadLibrary("dll");
}
public static native void addListener(Listener listener);
}
public interface Listener {
public void eventOccurred(Info info);
}
public Info {
private final String s1;
private final String s2;
public Info(String s1, String s2);
// ... getters for use in Java
}
Is it possible to
- register a
Listener
object on a dll (should be no problem, as far as I found out) - instantiate an
Info
object in the c/c++ code and use it as a parameter for callingListener.eventOccured(Info...)
?
Or what would be a good way to implement a listener which gets some information from a DLL?
In my case - we have a dll wich does some work. We call this dll from java. Now we want to attach a listener to the dll, to push us some progress information while working. The above example is the listener part, which I don't know if it is possible regarding the constructor call to a Java constructor from c/c++.
A hint where to find a piece of documentation, which describes the answer would be nice - I could not find infos, which answered my question.
A small snippet of code, describing the c/c++ part would be the icing on the cake :)
The short answer is yes, you can hold, instantiate, and pass Java objects in a native layer via JNI.
In the jni documentation, you will find functions that will do this for you. http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html
You will also Use
javap -s
andjavah
to assist in finding your java method signatures for use in jni and to make your jni headers. See http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html & http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javah.htmlHere is a sample of what you will be doing. I haven't tested it, so just use it as a reference for writing your own. Note: be aware of package notations(I've assumed no package) & Exceptions. Exceptions can happen on any JNI call(
env->...
), so check the documentation on how to deal with exceptions(ExceptionCheck
,ExceptionDescribe
,ExceptionClear
).