从C / C ++调用的Java方法 [英] Java methods calling from C/C++

查看:116
本文介绍了从C / C ++调用的Java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用C / C ++调用一些Java方法然后我需要在UVM或系统verilog中使用DPI来调用C方法。我无法访问JAVA文件或JAVA类。我有一个.jar文件,其中包含我需要访问哪些方法的类。



我正在使用该.jar文件的路径



options.optionString = - Djava。 class.path = .jar文件的路径



我可以找到课程使用

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");



但我无法获取方法ID。我只知道java类中方法的名称这是一个布尔参数的布尔方法


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);
}





这是我试图用来获取methodID的代码:



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.
}



请帮我解决这个问题,如何在c / c ++中访问此方法。还有一些方法我需要以相同的方式调用一旦我能够调用它将稍后尝试。我没有任何JAVA的想法我在网上得到这个代码作为如何从c / c ++调用java方法的例子。





我尝试了什么:



我创建了一个CTest.cpp文件,我刚刚为.jar文件提供了java的路径类。我只是用这个命令来执行.cpp文件



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


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

推荐答案

正常情况下JNI工作,你错过了一些细节。阅读文章使用JNI从C ++调用Java ,其中JNI是因为我一直在使用它。



检查包裹,类和功能是否公开。



最后,您可以尝试使用Java进行工作,只将导出结果作为字符串或数字等纯数据。
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.


这篇关于从C / C ++调用的Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆