Results 1 to 2 of 2
  1. #1
    H_P
    H_P is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default JNI accessing non primitive data type

    Hello

    I have a JNI method to access java method which returns an Integer object. I do not want to return the primitive int type because this code will be modified to handle Generic objects. The following is what I have. I am not able to get the value of the Integer that I pass. The output at C++ side is something like
    Java Code:
        value = 0x4016f3d0
    How can I get the actual value of Integer object that I pass at C++ end?

    Please help.

    Thanks,

    -H

    GenericPeer.cpp


    Java Code:
        JNIEXPORT void JNICALL Java_GenericPeer_print (JNIEnv *jenv, jclass jcls, jobject data){
         jclass peerCls = jenv->GetObjectClass(data);
         jmethodID mGetValue = jenv->GetMethodID(peerCls, "getValue","()Ljava/lang/Integer;");
         if(mGetValue == NULL){
           return (-1);
         } 
         jobject value = jenv->CallObjectMethod(data, mGetValue);
         cout<<"value = "<<value<<endl;
    
        }

    GenericPeer.java


    Java Code:
    public class GenericPeer {
        	public static native void print(Data d);
        	static {
        		System.load("/home/usr/workspace/GenericJni/src/libGenericJni.so");
        	}
        }
    Data.java

    Java Code:
    public class Data {
        	private Integer value;
        	pubilc Data(Integer v){ 
        		this.value = v;
        	}
        	public Integer getValue() { return value; }
           	public void setValue(Integer value) {
        	this.value = value;
        	}
        }

    Test.java (Main class)


    Java Code:
    public class Test {
        	public static void main(String[] args){
               Integer i = new Integer(1);
        		Data d = new Data(i);
        		GenericPeer.print(d);
        	    	}
        }

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    11

    Default

    The Integer class is not native. It's byte code. There is no acceptable way to access it as a c++ class except to write a c++ wrapper that makes jni calls back to the java world. The only other option is to write a byte code interpreter (or buy one, or partner with someone who has one).

    If you can explain your motivation for wanting the equivalent of *value, we might be able to help you come up with a different way of satisfying that objective.

Similar Threads

  1. primitive Data types
    By Manfizy in forum New To Java
    Replies: 2
    Last Post: 07-07-2009, 09:29 PM
  2. Primitive data types of Java
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:29 PM
  3. Accessing Data from a .txt file
    By Oasis13 in forum New To Java
    Replies: 5
    Last Post: 02-01-2008, 01:16 AM
  4. Uisng primitive type values as keys for Hashtable
    By ravian in forum New To Java
    Replies: 3
    Last Post: 11-21-2007, 11:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •