• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

what's wrong with my program about JNI callback

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the "Other Java APIs" forum.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
I was born with webbed fish toes. This tiny ad is my only friend:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic