Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have passed a hashmap from java to C. Now, I need to fill the hashmap and pass it back to java. This is my java code.
Main.java
import java.util.*;



public class Main {
	static {
		System.loadLibrary("Tests");	}

	    public void doProcess() {

	        Container container = new Container();
	        container.getParameterMap().put("foo","bar");

	        service(container);
	    }

	    public native void service(Container container);

   
	public static void main(String args[])
	{
	<pre>System.out.println("Service Names");
	   HashMap<String,String>[] h =  new HashMap().service();
	for(int i=0;;i++)
	  {
	      
		  System.out.print(h+"\n");
		
	  }

}

}

Container.java
import java.util.HashMap;
import java.util.Map;

public class Container {
	 private String hello;
	    private Map<String, String> parameterMap = new HashMap<String, String>();

	    public Map<String, String> getParameterMap() {
	        return parameterMap;
	    }
}



Now, how can I access and fill the hashmap and pass it back to java.

What I have tried:

I have tried getting hashmap in C using the following code.

jclass c_Container = (*env)->GetObjectClass(env, jContainer);

   jmethodID m_GetParameterMap = (*env)->GetMethodID(env, c_Container, "getParameterMap", "()Ljava/util/Map;");

   jobject jParameterMap = (*env)->CallObjectMethod(env, jContainer, m_GetParameterMap);


   jclass c_Map =(*env)->FindClass(env,"java/util/Map");

jmethodID put = (*env)->GetMethodID(env,c_Map, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

   jmethodID m_GetSize = (*env)->GetMethodID(env, c_Map, "size", "()I");


   int jSize = (*env)->CallIntMethod(env, jParameterMap, m_GetSize);
               char* key = (char*)"*";
             char* val = (char*)"";
            (*env)->CallObjectMethod(jParameterMap, put, key, val);


Now, can I access the hashmap and update the data and send it back.

Is there any in-built method does hashmap have to do this?
Posted
Updated 13-May-21 5:49am
v3

1 solution

You cannot use native methods to access a Java object in JNI. You can only use the Java types as defined in jni.h. So you have to figure out in your C code what the structure is and update it from that information. But it would be much simpler to use a simple aray and let the Java code rebuild the hashmap from the returned data.
 
Share this answer
 
Comments
[no name] 13-May-21 10:52am    
I have updated the code. I passed the hashmap from C to java(as a jObject). But, as you can see I need to know how to access that hashmap from java(defining method, and how to receive it). Could you possibly tell me how to do that?
Richard MacCutchan 13-May-21 11:58am    
You are using char* types in the C code to add fields to the Map. However in Java you defined it as Map<String, String>, so you need to pass JString types to the put method. As I already mentioned, you cannot use C types for Java objects.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900