JNI (C++ calls from Java)

T

the.new.delboy

Hi,

I'm working on an application which requires some native calls to be
made from Java. The calls work fine in one version of my code. I need
to change the location of my native calls to a different file but when
I move them to another native file I receive the following error. I've
included all .h includes and path locations (LD_LIBRARY_PATH) and the
build script contains the libraries required to run.

The strange thing: I removed the problematic 'readFrame' method from my
native code and still receive the same error...

java.lang.UnsatisfiedLinkError:
/home/delboy/acemedia/var/osgi/fwdir/bs/9/jar0/librvacetoolbox.so:
/home/delboy/acemedia/var/osgi/fwdir/bs/9/jar0/librvacetoolbox.so:
undefined symbol: _Z9readFramei
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1751)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1660)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:993)

Could this be a case of mangled C++ symbols?

Any suggestions would be great,

Del
 
D

David Harmon

On 7 Dec 2006 08:56:00 -0800 in comp.lang.c++, (e-mail address removed)
wrote,
java.lang.UnsatisfiedLinkError:
undefined symbol: _Z9readFramei
Could this be a case of mangled C++ symbols?

Seems like it could be... without knowing JNI, I guess that Z9readFramei
must be declared with the
extern "C"
linkage attribute in the C++ code. Is that true?
 
N

noone

On 7 Dec 2006 08:56:00 -0800 in comp.lang.c++, (e-mail address removed)
wrote,


Seems like it could be... without knowing JNI, I guess that Z9readFramei
must be declared with the
extern "C"
linkage attribute in the C++ code. Is that true?

You are correct.

Things you want to export from c++ must be defined
extern "C" so the names make sense to the foreign environment.
 
J

Jan Bornschlegel

noone said:
You are correct.

Things you want to export from c++ must be defined
extern "C" so the names make sense to the foreign environment.
Hej,

This should be done automatically if you used the "javah" to generate
the C++ header file.

I compiled a .java file containing the line
public native float makefloat( byte a, byte b, byte c, byte d );

and javah's output was:
#include <jni.h>
....
#ifdef __cplusplus
extern "C" {
#endif
....
JNIEXPORT jfloat JNICALL Java_connectivity_UdpConverter_makefloat
(JNIEnv *, jobject, jbyte, jbyte, jbyte, jbyte);

#ifdef __cplusplus
}
#endif

and in the appropriate cpp file I just implemented that method:

JNIEXPORT jfloat JNICALL Java_connectivity_UdpConverter_makefloat
(JNIEnv *env, jobject obj, jbyte byte1, jbyte byte2, jbyte byte3,
jbyte byte4)
{
....
}

The result project type is a dll which is loaded from the java file with:

static
{
System.loadLibrary( "ConvertByteArrayToFloat" );
}
before the first call.

I used that book
@book{Gor98,
author = {Gordon, Rob},
title = {{Essential JNI: Java Native Interface}},
edition = {1st},
publisher = {Prentice Hall Inc.},
address = {NJ, USA},
year = {1998}
}
for JNI help, and it is extremely well (and funny) written and I can
recommend it; you may want to look for a newer release, though.

Jan
 
T

the.new.delboy

Hi Jan,

That's correct...the auto generated header wraps extern "C" around the
JNIExport declarations.
Out of curiousity, I also added the extern "C" to my native code and it
seems to have made some progress (if you can call it that), I'm now
getting a similar error on a different function name...I was getting
tired of the old one anyway.

The new error looks like:

java.lang.UnsatisfiedLinkError:
/home/delboy/acemedia/var/osgi/fwdir/bs/9/jar0/librvacetoolbox.so:
/home/delboy/acemedia/var/osgi/fwdir/bs/9/jar0/librvacetoolbox.so:
undefined symbol: checkFileExists
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1751)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1660)
at java.lang.Runtime.loadLibrary0(Runtime.java:822)
at java.lang.System.loadLibrary(System.java:993)

I also tried using the linux ldd command but didn't find anything out
of place. I got my hands on a copy of Rob Gordon's book, it's currently
sitting on my desk but I intend to start reading soon...

Del
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top