Thanks for looking at the question. I am trying to call a java method which is in class files using the JNI interface. In turn, the called class file must execute another .jar file that is in the same pointer? It was not easy for me to do this, and I could not execute the .jar file. I mean, I cannot get the results from the fuile class available in the .jar file.
Can someone explain if this can be done, or should I look for another option?
The code is as follows:
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)
{
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");
}
cls = env->FindClass("F2C");
if( cls == NULL )
{
printf("can't find class F2C\n");
}
env->ExceptionClear();
}
void setMethodName(char* methodName)
{
cout<<"---------- Function Name is "<<methodName<<endl;
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;
}
.
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 file uses the herong.jar file
Please suggest if you have any ideas. Thanks, Asg