Java Native Interface (JNI) - Can I instantiate a Java Object from within my C/C++ native Code i

Hi.I am new to JNI.I would want to know if it is legal and doable to create anon-primitive Java Object (e.g. Date Object) inside my native codeand hand it back to the JVM, in case of JNI...?An explanation with a reasoning would be great.Thanks
[292 byte] By [sg678@yahoo.coma] at [2007-11-26 23:15:39]
# 1

Well, depends on what you mean by "instantiate a java object within"...

What you CAN do is have C/C++ code use the JNI to instantiate java objects. Those objects exist within the java JVM, but you can refer to them from C/C++.

What you want is called the "invocation API", and it is covered in the JNI tutorial.

bschauwejavaa at 2007-7-10 14:15:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

hello bschauwejava

i am new to JNI, i declared a native function in java with return type as a java bean class. I invoked the constructor and the setter method of the bean in the implementation of the native method in C. After setting the struc values in C to that of the invoked setter methods, i returned the java class object from the method. Now i am not getting how to catch the returned object in Java and retrieve the values that are set in native method. Can u please help me out it is urgent

kots@indiaa at 2007-7-10 14:15:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

If I understtod what you said, you have some java that calls a native method:

// Like this?

doSomethingInC();

.....

public native Classx doSomethinginC();

.....

Your C code constructs and returns a Classx

// Instead, do it like this:

Classx x = doSomethingInC();

x.getSomeFieldOrOther();

...

bschauwejavaa at 2007-7-10 14:15:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

thanks of your response, i already tried that approach, its throwing error Exception in thread "main" java.lang.AbstractMethodError: sample1struc.getSCPname()Ljava/lang/String;

please have a look how i implemented.

import java.io.*;

public class sample1struc

{

public String SCPname;

public void sample1struc()

{};

public void setSCPname(String SCPname)

{

this.SCPname = SCPname;

}

public String getSCPname()

{

return SCPname;

}

}

java Program through which above bean called:

public class sample1

{

public native sample1struc readFile();

static

{

System.loadLibrary("sample1");

}

public static void main(String []args)

{

sample1 sm1 = new sample1();

sample1struc xx = sm1.readFile();

System.out.println(xx.getSCPname());

}

}

The C++ native method implementation:

JNIEXPORT jobject JNICALL Java_sample1_readFile

(JNIEnv *env, jobject obj)

{

struct emp e;

e.no = 1;

strcpy(e.name,"Siva");

jclass cls = env->FindClass("sample1struc");

jmethodID mid = env->GetMethodID(cls,"<init>","()V");

jobject xinstance = env->NewObject(cls,mid);

jmethodID setterid = env->GetMethodID(cls,"setSCPname","(Ljava/lang/String;)V");

xinstance = env->NewObject(cls,setterid,&e.name);

return cls;

}

On running the sample1.java i get the above stated exception. How to handle this scenario.

Please suggest me how to implement the same scenario if i need to pass a structure array from C. How do i call the setter method in loop and what will be the return object of the method.

In regular j2ee programs, i use list class while setting the values, based on the property i iterate the List and display them on JSP.

How to do that using JNI, when i need to display values from C++ structure array to JSP.

applogies for lengthy post.

kots@indiaa at 2007-7-10 14:15:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5
You are retruning the class, when you should be returning the instance.
bschauwejavaa at 2007-7-10 14:15:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6

thanks for your response.

i could not get what you are saying.

My understanding is that, cls is an instance of class sample1struc.

plz check this

jclass cls = env->Findclass("sample1struc");

i am sorry, if my understanding is sorry.

what you mean by instance, i cannot return the instance of method.

Even i tried calling the getter method in C++ native code. In run time it is throwing No such method found error.

kots@indiaa at 2007-7-10 14:15:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...