Can C++ Application load the .jar File using JNI?(C++ 应用程序可以使用 JNI 加载 .jar 文件吗?)
问题描述
Thanks for having a look at question. I am trying to invoke a java method which is in class files using JNI interface. In turn the called class file should be executing the another .jar file which resides in the same direcotry ? I had hard time acheiving this and I am unsuccessful in executing the .jar file. I mean I am not able to get the results from the class fuile available in .jar file.
Can any one explain,whether it is possible to do that way or I should look for the another option ?
The code is like this:
class JNIInterface
{
private:
JavaVMInitArgs vm_args;
JavaVM *jvm;
JNIEnv *env;
long result;
jmethodID mid;
jfieldID fid;
jobject jobj;
jclass cls;
int asize;
char JVMOptionString[20];
char className[20];
char methodName[20];
JavaVMOption options[1];
public:
JNIInterface(char* JVMOptionString)
{
// JavaVMOption options[1];
options[0].optionString = JVMOptionString;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.version = JNI_VERSION_1_6;
vm_args.ignoreUnrecognized = JNI_FALSE;
}
void setClassName(char* className)
{
result = JNI_CreateJavaVM( &jvm,(void **)&env, &vm_args);
if(result == JNI_ERR )
{
printf("Error invoking the JVM
");
//return 0;
}
cls = env->FindClass("F2C");
if( cls == NULL )
{
printf("can't find class F2C
");
//return 0;
}
env->ExceptionClear();
}
void setMethodName(char* methodName)
{
cout<<"---------- Function Name is "<<methodName<<endl;
//---------- Integer Value with Parameter ----------------
mid=env->GetStaticMethodID(cls, methodName, "([Ljava/lang/String;)V");
if (mid != NULL)
{
env->CallStaticVoidMethod(cls,mid,"70");
}
int main()
{
JNIInterface JNIInterfaceObj("-Djava.class.path=C:\MyPOC;C:\MyPOC\herong.jar");
JNIInterfaceObj.setClassName("F2C");
JNIInterfaceObj.setMethodName("main");
return 0;
}
.
//The java file which is calling jar files is - F2C.java
/**
* F2C.java
* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
*/
import herong.TempratureConvertorBean;
public class F2C {
public void test(String[] arg) {
try {
double f = 0.0;
System.out.println("Inside test func:");
TempratureConvertorBean b = new TempratureConvertorBean();
if (arg.length>0) f = Double.parseDouble(arg[0]);
b.setFahrenheit(f);
double c = b.getCelsius();
System.out.println("Fahrenheit = "+f);
System.out.println("Celsius = "+c);
System.out.println(b.getInfo());
}
}
public static void main(String[] arg) {
F2C f2c = new F2C();
f2c.test(arg);
}
}
this F2C.java uses the herong.jar file
Please suggest if you have any idea. Thanks, Asg
Your question isn't completely clear, but I will give a general answer...
In Java there is only two ways to get Java to look in a .jar file (and they really boil down to one way in the end), and that's to specify the .jar file in the classpath, or to create a classloader that will look in that jar file and add it to the list of classloaders Java will use.
And, of course, all the classpath is is a set of classloaders that Java instantiates and uses before your program even starts.
So a JNI program needs to make the Java calls (which I'm not looking up just now) to set up a new class loader and get Java to start using it if the JNI program needs Java to start looking in additional .jar files.
这篇关于C++ 应用程序可以使用 JNI 加载 .jar 文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 应用程序可以使用 JNI 加载 .jar 文件吗?
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01