C ++应用程序可以使用JNI加载.jar文件吗? [英] Can C++ Application load the .jar File using JNI?
问题描述
感谢您查看问题。
我试图使用JNI接口调用类文件中的java方法。反过来,被调用的类文件应该执行另一个驻留在同一个目录中的.jar文件?我很难实现这一点,但我没有成功执行.jar文件。我的意思是我无法从.jar文件中获得类fuile的结果。
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 ?
代码是这样的:
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\n");
//return 0;
}
cls = env->FindClass("F2C");
if( cls == NULL )
{
printf("can't find class F2C\n");
//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);
}
}
此F2C.java使用herong.jar文件
this F2C.java uses the herong.jar file
如果您有任何想法,请提出建议。
谢谢,
Asg
Please suggest if you have any idea. Thanks, Asg
推荐答案
你的问题不完全清楚,但我会给一个一般的回答...
Your question isn't completely clear, but I will give a general answer...
在Java中,只有两种方法可以让Java在.jar文件中查找(并且它们最终归结为一种方式),那就是在类路径中指定.jar文件,或者创建一个类浏览器,它将查看该jar文件并将其添加到Java将使用的类加载器列表中。
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.
当然,所有类路径都是一组Java实例化并在程序启动之前使用的类加载器。
And, of course, all the classpath is is a set of classloaders that Java instantiates and uses before your program even starts.
所以JNI程序需要创建Java如果JNI程序需要Java开始查找其他.jar文件,那么调用(我现在不想查看)来设置新的类加载器并让Java开始使用它。
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文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!