How to return value from Java to C using JNI?

How to return value from Java to C using JNI?

Page 1 of 1

2 Replies - 4517 Views - Last Post: 06 March 2009 - 12:36 PM Rate Topic: -----

#1 ketu1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-March 09

How to return value from Java to C using JNI?

Post icon  Posted 06 March 2009 - 12:04 AM

Hi
I have written code to call java funvction from C.
Program runs successfully when return type of Java function is void.
Program crashesh if return type of java function is changed to int. and return 100 statement is added at the end.


In C program , value is recieved in jint as
jiReturn = (*env)->CallStaticIntMethod(env, stringClass2, id2_main, args2);
printf("C code has received value %d from JAVA .",jiReturn);

When return type of java method is void and no return statement is written in java code, garbage value is printed for jiReturn.

However, if java program is changed to return value instead of void, program crashes.

java code is -
public class nature
{
  public static int abc(String[] args)
  {
   		System.out.println("Hello JAVA");		
		return 100;
}
public static void main (String[] args)
{

}
}



function abc is called from C code.
It works well and prints garbage value when return type is void as below :
public class nature
{
  public static void abc(String[] args)
  {
   		System.out.println("Hello JAVA");		
		
}
public static void main (String[] args)
{

}
}



Can you please help?

Is This A Good Question/Topic? 0
  • +

Replies To: How to return value from Java to C using JNI?

#2 ketu1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-March 09

Re: How to return value from Java to C using JNI?

Posted 06 March 2009 - 01:20 AM

View Postketu1, on 5 Mar, 2009 - 11:04 PM, said:

Hi
I have written code to call java funvction from C.
Program runs successfully when return type of Java function is void.
Program crashesh if return type of java function is changed to int. and return 100 statement is added at the end.


In C program , value is recieved in jint as
jiReturn = (*env)->CallStaticIntMethod(env, stringClass2, id2_main, args2);
printf("C code has received value %d from JAVA .",jiReturn);

When return type of java method is void and no return statement is written in java code, garbage value is printed for jiReturn.

However, if java program is changed to return value instead of void, program crashes.

java code is -
public class nature
{
  public static int abc(String[] args)
  {
   		System.out.println("Hello JAVA");		
		return 100;
}
public static void main (String[] args)
{

}
}



function abc is called from C code.
It works well and prints garbage value when return type is void as below :
public class nature
{
  public static void abc(String[] args)
  {
   		System.out.println("Hello JAVA");		
		
}
public static void main (String[] args)
{

}
}



Can you please help?



problem is resolved after changing line as below :
id2_main = (*env)->GetStaticMethodID(env, stringClass2,"abc", "([Ljava/lang/String;)I");

Was This Post Helpful? 0
  • +
  • -

#3 skyHigh  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 116
  • Joined: 01-October 07

Re: How to return value from Java to C using JNI?

Posted 06 March 2009 - 12:36 PM

by any chance have u done the other way around. Java calls C++? because I need to call some C++ functions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1