Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

what's wrong with my program about JNI callback

 
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?
 
mister krabs
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
Beware the other head of science - it bites! Nibble on this message:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic