Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » jni problem(unsatisfied link error)
jni problem [message #686313] Tue, 21 June 2011 22:23 Go to next message
James Wolfe is currently offline James Wolfe
Messages: 5
Registered: June 2011
Junior Member
I am learning to use JNI.
I created a java source file, a header file and the shared object library all outside
of eclipse.
Everything runs fine.

Next I imported the java source into a package of my eclipse project.
Eclipse compiled the java source.
I then went to the bin directory in the eclipse project.
I ran javah to create the header file.
I copied the header file to the project package folder.
I compiled the shared object library from the command line (gcc...).

The loadLibrary seems to work, but the call to the native function is not found.
Java is looking for a native function with the java package name as a prefix.

java.lang.UnsatisfiedLinkError: irSensor.ExternalSignatureNoiseCCPP.printfString(Ljava/lang/String;D)I

The shared object library does not know about the package name.

Am I going about this all wrong.

I have the CDT plugin installed but for starters I thought I would try the approach
above.

Function call code..............................

public boolean doNoiseRawSignature(String command, DiscrimConfig config,
MyJFrame myJF, String mode) {

boolean error = false;
Map<String, Signature> rawSigs = config.getSignaturesRaw();
MyLog.logStartOfOperation(command + ": " + "Raw "
+ "C / C++", myJF);
Random rand = new Random();

ExternalSignatureNoiseCCPP esnccpp = new ExternalSignatureNoiseCCPP();
int count = 0;

System.out.println("\n" + "Java sent a string and a double.\n");
count = esnccpp.printfString("pi = ", 3.14159265);

System.out.println("java received an integer: " + count);

System.out.println();


Java source .......................................

package irSensor;

public class ExternalSignatureNoiseCCPP {

static {
System.loadLibrary("ExternalSignatureNoiseCCPP");
}
public ExternalSignatureNoiseCCPP(){

}
public native int printfString(String s, double x);

}


C code ............................................

#include <jni.h>
#include "ExternalSignatureNoiseCCPP.h"
#include <stdio.h>

JNIEXPORT jint JNICALL Java_ExternalSignatureNoiseCCPP_printfString
(JNIEnv *env, jobject obj, jstring str, jdouble x)
{
jboolean iscopy;
const char *message = (*env)->GetStringUTFChars(
env, str, &iscopy);
printf("C received:\n%s%f\n\n", message, x);
jint ret;
ret = 1;
fflush(stdout);
return ret;
}


Thanks, Paul
Re: jni problem [message #686373 is a reply to message #686313] Wed, 22 June 2011 02:12 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam Kandula
Messages: 444
Registered: July 2009
Senior Member
Did you move the class into the package after using javah? I believe the native function should have been Java_irSensor_ExternalSignatureNoiseCCPP_printfString.
Re: jni problem [message #686628 is a reply to message #686313] Wed, 22 June 2011 12:01 Go to previous messageGo to next message
James Wolfe is currently offline James Wolfe
Messages: 5
Registered: June 2011
Junior Member
Thanks for the reply.

I did as you suggested. I renamed the c function to include the package name and
all is well.

I am still unclear as to how to run javah on a class that is a member of a package.

I tried,

javah -jni ExternalSignatureNoiseCCPP

in the bin directory containing the class file.

I got,

error: cannot access ExternalSignatureNoiseCCPP
bad class file: RegularFileObject[./ExternalSignatureNoiseCCPP.class]
class file contains wrong class: irSensor.ExternalSignatureNoiseCCPP
Please remove or make sure it appears in the correct subdirectory of the classpath.

I tried,

javah -jni irsensor.ExternalSignatureNoiseCCPP

I got

error: cannot access irsensor.ExternalSignatureNoiseCCPP
class file for irsensor.ExternalSignatureNoiseCCPP not found

So, to restate my question.

What is the command to run javah on a class file that came from a source in some package.

Thanks again, paul
Re: jni problem [message #687741 is a reply to message #686628] Thu, 23 June 2011 02:00 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam Kandula
Messages: 444
Registered: July 2009
Senior Member
Run javah -jni irsensor.ExternalSignatureNoiseCCPP
from the bin folder and not from irsensor.
Re: jni problem [message #688583 is a reply to message #686313] Fri, 24 June 2011 21:17 Go to previous message
James Wolfe is currently offline James Wolfe
Messages: 5
Registered: June 2011
Junior Member
Thanks for the help. The solution was to run javah from the project root
and to add a class path to the bin directory.

cd ../bin
javah -classpath ./bin irSensor.ExternalSignatureNoiseCCP

Paul
Previous Topic:Eclipse  Java EE IDE
Next Topic:Java Version Compile Mismatch 1.5/1.6
Goto Forum:
  


Current Time: Fri Aug 22 05:56:38 EDT 2014

Powered by FUDForum. Page generated in 0.01729 seconds