aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes what's wrong with my program about JNI callback Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "what Watch "what New topic
Author

what's wrong with my program about JNI callback

zooly xue
Greenhorn

Joined: Aug 19, 2003
Posts: 5
THE FELLOWING IS MY JAVA PROGRAM:
////////////////////////////////////////////////////////////////////////////
public class JNITest{
/*native methods*/
private native void calculate();
/*the difination of fcn,and the JNI method calculate call it*/
void fcn(int m, int me, int n, float x[], float f,
float g[])
{
float tmp1, tmp2;
tmp1 = x[0] - 2;
tmp2 = x[1] - 1;
f = tmp1 * tmp1 + tmp2 * tmp2;
//if (active[0]),without c.lib,can't be used
g[0] = x[0] - 2 * x[1] + 1;
//if (active[1])
g[1] = -(x[0]*x[0]) / 4 - x[1]*x[1] + 1;
System.out.println("f:"+ f);
System.out.println("g[0]"+g[0]);
System.out.println("g[1]"+g[1]);
return;
}
public static void main(String []args)
{
JNITest test=new JNITest();
test.calculate();
}
static{
System.loadLibrary("JNITest");
}
}
////////////////////////////////////////////////////////////////////////////
AND THE FELLOWING IS MY PROGRAM WRITTEN WITH VISUAL C++ 6.0
////////////////////////////////////////////////////////////////////////////
#include<jni.h>
#include<stdio.h>
#include"E:\my package\JNITest.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JNITest
* Method: calculate
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_JNITest_calculate
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#define M 2
#define ME 1
#define N 2
JNIEXPORT void JNICALL
Java_JNITest_calculate(JNIEnv *env,jobject obj)
{
/*then access the methods fcn*/
jclass cls=(env)->GetObjectClass(obj);
jmethodID mid=(env)->GetMethodID(cls,"fcn","(III[FF[F V");
if(mid==NULL){
return;
}
/*translate my own para*/
jfloat x[]={2,2};
jfloat f=0;
jfloat g[2];
/*call the Java fcn*/
printf("In C\n");
(env)->CallVoidMethod(cls,mid,M,ME,N,x,f,g);
}
////////////////////////////////////////////////////////////////////////////
javac JNITest.java :succeed
javah -jni JNITest :succeed
compile the C program into JNITest.dll :succeed
java JNITest :false
the false description:
Exception in thread "main" java.lang.NoSuchMethodError: fcn
at JNITest.calculate<Native method>
at JNITest.main<JNITest.java:9>
////////////////////////////////////////////////////////////////////////////
WHAT'S WRONG WITH MY PROGRAM?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If you want someone to read your code, please properly indent it and use the [ code ] tags.
http://www.javaranch.com


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Moving to the "Other Java APIs" forum.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

As Tom mentioned, the first problem is that it is almost unreadable!

The other problem is that you can't treat java arrays in C like you treat arrays in Java... you have to replace -


with -


I noticed a few other problems (like the method signature "(III[FF[F V" should be "(III[FF[F)V" ), but this is the most major correction.

Post again if you have more trouble...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what's wrong with my program about JNI callback