JNI problems

D

Daniel Dyer

Hello,

I am having some problems with using some packages and my JNI
application.

I get the famous error

Exception in thread "Thread-1" java.lang.UnsatisfiedLinkError: printMe
at VEViewer.ParticleSLAMProxy.printMe(Native Method)
at VEViewer.J3DARToolkit.run(J3DARToolkit.java:301)

Where there is package called VEViewer and a class called
ParticleSLAMProxy. This class calls the native method printMe in a c++
file :

//C++ file
JNIEXPORT void JNICALL Java_VEViewer_ParticleSLAMProxy_printMe
(JNIEnv * env, jobject obj)
{
printf ("Print Me \n");
}

The library (temp.dll) is found by Java, but the problem is that the
declaration of this native method is not found. I am running this
program from a jar file.

Do you have an idea about what I am doing wrong?

JNI uses C calling conventions. The C++ compiler will mangle your
function names if you don't wrap your C++ code in the following:

#ifdef __cplusplus
extern "C" {
#endif

/* JNI functions go here. */

#ifdef __cplusplus
}
#endif


Dan.
 
M

M. Fernandez

Hello,

I am having some problems with using some packages and my JNI application.

I get the famous error

Exception in thread "Thread-1" java.lang.UnsatisfiedLinkError: printMe
at VEViewer.ParticleSLAMProxy.printMe(Native Method)
at VEViewer.J3DARToolkit.run(J3DARToolkit.java:301)

Where there is package called VEViewer and a class called
ParticleSLAMProxy. This class calls the native method printMe in a c++
file :

//C++ file
JNIEXPORT void JNICALL Java_VEViewer_ParticleSLAMProxy_printMe
(JNIEnv * env, jobject obj)
{
printf ("Print Me \n");
}

The library (temp.dll) is found by Java, but the problem is that the
declaration of this native method is not found. I am running this
program from a jar file.

Do you have an idea about what I am doing wrong?

thanks a lot,

Marcelo
 
G

Gordon Beaton

JNI uses C calling conventions. The C++ compiler will mangle your
function names if you don't wrap your C++ code in the following:

#ifdef __cplusplus
extern "C" {
#endif

/* JNI functions go here. */

#ifdef __cplusplus
}
#endif

While your comment is perfectly correct, I'd just like to add that if
you use the exact names and signatures generated by javah and also
include the generated header file, then the functions will already be
declared extern "C".

The OP also needs to watch out for another kind of mangling typical
for MS windows, described here:

http://en.wikipedia.org/wiki/Name_mangling

Use an inspection tool to see what the library really contains after
compiling.

Finally, it's important to realize that if a package declaration was
used in the Java source, then the header and the native source files
should reflect that (the fully qualified classname should have been
specified when generating the header file).

/gordon
 
M

M. Fernandez

Thanks a lot for your help,
I have found my error with your useful remarks. Actually I has been
accessing the java objects in a bad was. I was calling the JNI functions
with a "static method" and this is obviously wrong (because I have
declared them as object methods).

I just have another question, Do you know an inspector tool for the dll
libraries (Windows, Linux or multi-platform ) ??

thanks,

MArcelo
 
G

Gordon Beaton

I just have another question, Do you know an inspector tool for the
dll libraries (Windows, Linux or multi-platform ) ??

On Linux and all Unix dialects: objdump and nm.

I don't use Windows, but others have mentioned things like dumpbin,
quickview and depends, which I know nothing about.

/gordon
 
C

Chris Uppal

Gordon said:
The OP also needs to watch out for another kind of mangling typical
for MS windows, described here:

http://en.wikipedia.org/wiki/Name_mangling

The JVM is aware of that kind of decoration and checks (on Windows) for both
decorated and undecorated forms of the name:
_some_function@8
and
some_function

It does something similar on other OSs.

Use an inspection tool to see what the library really contains after
compiling.

Agreed, most especially if one is using an "unusual" compiler

-- chris
 

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,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top