This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what's wrong with my program about JNI callback

 
zooly xue
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want someone to read your code, please properly indent it and use the [ code ] tags.
http://www.javaranch.com
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the "Other Java APIs" forum.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic