By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 1,852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

JNI (C++ calls from Java)

P: n/a
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.jav a: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

Dec 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 7 Dec 2006 08:56:00 -0800 in comp.lang.c++, th************@gmail.com
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?

Dec 7 '06 #2

P: n/a
On Thu, 07 Dec 2006 17:11:53 +0000, David Harmon wrote:
On 7 Dec 2006 08:56:00 -0800 in comp.lang.c++, th************@gmail.com
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?
You are correct.

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

Dec 8 '06 #3

P: n/a
noone schrieb:
On Thu, 07 Dec 2006 17:11:53 +0000, David Harmon wrote:
>On 7 Dec 2006 08:56:00 -0800 in comp.lang.c++, th************@gmail.com
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?

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
Dec 8 '06 #4

P: n/a
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.jav a: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

Jan Bornschlegel wrote:
noone schrieb:
On Thu, 07 Dec 2006 17:11:53 +0000, David Harmon wrote:
On 7 Dec 2006 08:56:00 -0800 in comp.lang.c++, th************@gmail.com
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?
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
Dec 8 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.