Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to call some Java methods using C/C++ and then I need to use DPI in UVM or system verilog to call C methods. I dont have access for JAVA files or JAVA classes. I have a .jar file in which my class is present of which methods I need to access.

I am using path of that .jar file in

options.optionString = "-Djava.class.path= path of .jar file

I am able to find class using
clsH = env->FindClass("com/act/testdev/pa5/PA5");

But I am not able to get method ID. I know only method's name in java class which is a boolean method with boolean arguments
com.act.testdev.pa5.PA5.aes_encrypt(key_data, pt_data);

This method is used in some other language and here is the code of that:
boolean[] aes_encrypt(boolean key_data[], boolean pt_data[]) 
{
    return com.act.testdev.pa5.PA5.aes_encrypt(key_data, pt_data);
}


Here is the code which I am trying to use to get the methodID:
if (clsH != NULL)
{
    //midMain       = env->GetMethodID(clsH, "<init>", "([Z[Z)[Z");    
    midMain       = env->GetMethodID(clsH, "aes_encrypt", "([Z[Z)[Z"); 
    printf("\n FOUND able to find the requested class midMain ==%x ==%x\n",midMain,clsH);       
}
else
{
    printf("\nUnable to find the requested class\n");       
}

if (midMain != NULL)
{
    //jstring StringArg = env->NewStringUTF("\nTestCall:Called from the C Program\n");
    printf("\nable to find the requested Method\n");        
    jbooleanArray BoolArg1 = env->NewBooleanArray(128);
    jbooleanArray BoolArg2 = env->NewBooleanArray(128);
    env->CallBooleanMethod(clsH,midMain,BoolArg1,BoolArg2);
    //env->CallStaticVoidMethod(clsH, midMain, NULL); //Calling the main method.
    //env->CallBooleanMethod(clsH, midMain, BoolArg); //Calling the main method.
}

Please help me on this how can I access this method in c/c++. there are some more methods also I need to call in same way once I am able to call this will try those later. I dont have any Idea of JAVA I got this code online as a example of how to call java methods from c/c++.


What I have tried:

I have created a CTest.cpp file in that I have just given path for .jar file which has java classes. and I am just using this command to execute the .cpp file

g++ -I/soft/uo/jdk1.8.0_11/include -I/soft/uo/jdk1.8.0_11/include/linux -L/soft/uo/jdk1.8.0_11/jre/lib/amd64/server -ljvm CTest.cpp
Posted
Updated 12-Jun-18 1:24am
v2

1 solution

Normally JNI works and you have missed some details. Read the article Calling Java from C++ with JNI in which the JNI is used as I had been working with it.

Check that the packages, classes and functions are public.

At last you can try to do your work in Java and only export the results as plain data like strings or numbers.
 
Share this answer
 
Comments
Manish Sharma 18-Jun-18 6:27am    
clsH = env->FindClass("com/act/testdev/pa5/PA5");clsH1=env->FindClass("com/gate/sam/Samlet$BooleanArray");if (clsH1 != NULL){printf("\n FOUND able to find the requested class ==%x \n",clsH1);}else{printf("\nUnable to find the requested class\n");}if (clsH != NULL){midinit=env->GetMethodID(clsH, "<init>", "(III[I)V");midMain =env->GetMethodID(clsH, "aes_encrypt", "([Lcom/gate/sam/Samlet$BooleanArray;[Lcom/gate/sam/Samlet$BooleanArray;)[Lcom/gate/sam/Samlet$BooleanArray");printf("\n FOUND able to find the requested class midMain ==%x ==%x\n",midMain,clsH); }

in this code able to find <init> method of PA5 class but still not able to find aes_encrypt method. I found that for Boolean Array there is another class. So I tried to link that calss in arguments and return type but still its not working. I am able to find boolean array class in cIsH1 handle.

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