Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can C++ Application load the .jar File using JNI?

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

this F2C.java uses the herong.jar file

Please suggest if you have any idea. Thanks, Asg

like image 375
CPPP Avatar asked Oct 26 '22 22:10

CPPP


1 Answers

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.

like image 103
Omnifarious Avatar answered Nov 09 '22 08:11

Omnifarious