java


JNI - Listener in C++/Java - is it possible to instantiate Java objects in c++ and use them as parameters


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 calling Listener.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 and javah 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.html
Here 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).
JavaVM * savedVM = NULL;
JNIEXPORT void JNICALL Java_NativeClass_addListener(JNIEnv *env, jobject obj_instance, jobject listener_instance)
{
env->GetJavaVM( &savedVM );
//save listener_instance for use later
saved_listener_instance = listener_instance;
}
void doSomething()
{
//Get current thread JNIEnv
JNIEnv * ENV;
int stat = savedVM->GetEnv((void **)&ENV, JNI_VERSION_1_6);
if (stat == JNI_EDETACHED) //We are on a different thread, attach
savedVM->AttachCurrentThread((void **) &ENV, NULL);
if( ENV == NULL )
return; //Cant attach to java, bail
//Get the Listener class reference
jclass listenerClassRef = ENV->GetObjectClass( saved_listener_instance );
//Use Listener class reference to load the eventOccurred method
jmethodID listenerEventOccured = ENV->GetMethodID( listenerClassRef, "eventOccurred", "(LInfo;)V" );
//Get Info class reference
jclass infoClsRef = ENV->FindClass( "Info" );
//Create Info class
jobject info_instance = ENV->NewObject( infoClsRef, ..... );//For you to fill in with your arguments
//invoke listener eventOccurred
ENV->CallVoidMethod( saved_listener_instance, listenerEventOccured, info_instance );
//Cleanup
ENV->DeleteLocalRef( info_instance );
}

Related Links

Java: Filtering Panels like a JTable
Auto Extracting XPATH of all html elements using JAVA
How to convert em dash in java
Is Java's import keyword for source files or binary files?
PostgreSQL function to insert into tables.. i need it to return specific values on errors and on specific if conditions
Have to click twice to get number, Java program not working as intended
Eclipse Java Compiler Errors
JSPExpression Language reading value from bean
Switch case not returning string
Get selected cell object in datatable using different lists for each column [duplicate]
Trouble getting a Java Applet program to open in my browser
NoNodeAvailableException[None of the configured nodes are available] in Elasticsearch 5.1.2
SPRING #RestController: Passing Cookie to a Service from Every Method
Updating version numbers of modules in a multi-module Maven project where the aggregate root isn't a parent pom
How can I use dagger 2 so that I can use an abstract representation of shared preferences?
mock method is not getting called - java

Categories

HOME
google-chrome-extension
coq
openmp
webpack
urbancode
plot
react-virtualized
framework7
getelementsbytagname
yarn
jpeg
ckeditor
echarts
pheatmap
adobe-analytics
node-pdfkit
fancybox-3
user-input
kentor-authservices
spring-xd
u-sql
google-cloud-ml
size
dtrace
hex-editors
serilog
jqwidget
shared-hosting
jtextfield
grails3
paging
cx-freeze
clickonce
icloud-api
java-7
facet
fopen
assistant
atlassian-crucible
libssl
google-cloud-nl
data-manipulation
perlin-noise
amazon-kinesis-kpl
bower-install
swisscomdev
withings
starteam
sharefile
ssjs
jna
ws-security
arena-simulation
eclipse-gef
long-polling
errordocument
fancybox-2
git-diff
np-complete
ifs
cudafy.net
flutterwave
mcafee
redundancy
or-tools
migradoc
removechild
connect-by
prettytensor
blackberry-10
xpath-1.0
cloudbees
hsv
sdhc
icu4j
wireshark-dissector
jqgrid-formatter
sysinternals
mmc
system32
key-management
graph-api-explorer
dereference
nsmutabledictionary
coldbox
java.util.concurrent
php-5.4
expected-exception
access-rights
dir
quartz-graphics
bulkloader
yui-compressor
mqx
wsdl-2.0
javaspaces
custom-backend
libc++
qt-jambi
mysql-error-1005
sudzc
icanhaz.js
photoshop-cs4
privilege
ubuntu-9.04

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App