如何在JNI C ++环境中添加.jar依赖文件

时间:2015-04-24 22:15:48

标签: java c++ java-native-interface

所以,我一直试图找到一种方法来运行我的java代码及其在Visual C ++程序中的依赖.jar文件。

我使用以下代码在C ++中运行java程序,没有任何问题。

Java代码

public class Sample2
{
 public static int intMethod(int n)
 {
   return n*n;
 }
  public static boolean booleanMethod(boolean bool) 
  {
   return !bool;
  }
}

C ++代码

// 1.cpp:主项目文件。

 #include "stdafx.h"
 #include "jni.h"
 #include <windows.h>
 using namespace System;


int CallJava()
 {
  JavaVM *jvm;   /* denotes a Java VM */

  JNIEnv *env;  /* pointer to native method interface */

  jint square;

  jboolean not;

  JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */

  JavaVMOption *options = new JavaVMOption[1];

  options[0].optionString = "-Djava.class.path=C:\\JavaCode";  

  vm_args.version = JNI_VERSION_1_6;
  vm_args.options = options;
  vm_args.nOptions = 1;
  vm_args.ignoreUnrecognized = false;

  HINSTANCE hinstLib;  

   hinstLib = LoadLibrary(TEXT("C:\\Program   Files\\Java\\jdk1.6.0_45\\jre\\bin\\server\\jvm.dll"));

 if(hinstLib==0)
 {
  printf("Error");
  }

  if(hinstLib!= NULL)
 {
  typedef jint (JNICALL *PtrCreateJavaVM)(JavaVM **, void **, void *);

   PtrCreateJavaVM ptrCreateJavaVM =     (PtrCreateJavaVM)GetProcAddress(hinstLib,"JNI_CreateJavaVM");

  int res = ptrCreateJavaVM(&jvm, (void**)&env, &vm_args);

   jclass cls = env->FindClass("Sample2");

   jmethodID mid;   

  if(cls !=0)
  {   
   mid = env->GetStaticMethodID(cls,"intMethod","(I)I");
    if(mid !=0)
   {  
     square = env->CallStaticIntMethod(cls, mid, 5);       
     printf("Result of intMethod: %d\n", square);
   }

  mid = env->GetStaticMethodID(cls, "booleanMethod", "(Z)Z");
  if(mid !=0)
  { 
    not = env->CallStaticBooleanMethod(cls, mid, 1);
    printf("Result of booleanMethod: %d\n", not);
    }
 }
  jvm->DestroyJavaVM();
 }
else
{
  printf("Library is NULL");
}
   Console::Read();
   return 0;
 }

 int main(array<System::String ^> ^args)
 {
 CallJava();
 Console::Read();
 return 0;
 }

现在,我的问题是,因为我的下面的java程序使用了一个目录中的几个jar文件。

Java代码

 import javax.swing.*;
 import es.unex.sextante.core.Sextante;
 import es.unex.sextante.gui.core.SextanteGUI;

 public class Demo5 
 {

 public static class Starter implements Runnable 
 {
     public void run() 
     {
        Sextante.initialize();
        SextanteGUI.getGUIFactory().showModelerDialog();

     }
  }

    public static class GUI implements Runnable 
   {
     public void run() 
     {
        try 
        {
           SwingUtilities.invokeAndWait(new Starter());
        }
        catch (Exception exc) 
        {
            throw new RuntimeException(exc);
        }
     }
 }

   public static void run() 
   {
    Thread gui = new Thread(new GUI());
    gui.start();
   }   

 }

我能够通过命令提示符使用以下方法编译和运行java代码:

编译:         javac -cp c:\ Lib \ sextante.jar; c:\ Lib \ sextante_gui.jar; Demo5.java

执行命令         java -cp c:\ Lib \ sextante.jar; c:\ Lib \ sextante_gui.jar; Demo5

但是,我不知道如何以JNI方法添加jar文件。

I tried the following ways: 

一个。方法1:以下方法不起作用:

    JavaVMOption *options = new JavaVMOption[3];
    options[0].optionString = "-Djava.class.path=C:\\JavaCode";  
    options[1].optionString = "-Djava.class.path=C:\\Lib\\sextante.jar"; 
    options[2].optionString = "-Djava.class.path=C:\\Lib\\sextante_gui.jar"; 

湾方法2:我试图在方法调用中找到jar ...

没有什么工作正常。你能帮我解决一下这个问题吗?

感谢。

1 个答案:

答案 0 :(得分:0)

代码:

WebDriverException: Message: 'chromedriver' executable needs to be available in the path. Please look at     http://docs.seleniumhq.org/download/#thirdPartyDrivers and read up at http://code.google.com/p/selenium/wiki/ChromeDriver

将转换为命令

  

java.exe -Djava.class.path = C:\ JavaCode -Djava.class.path = C:\ Lib \ sextante.jar -Djava.class.path = C:\ Lib \ sextante_gui.jar

您可能想要做的是:

JavaVMOption *options = new JavaVMOption[3];
options[0].optionString = "-Djava.class.path=C:\\JavaCode";  
options[1].optionString = "-Djava.class.path=C:\\Lib\\sextante.jar"; 
options[2].optionString = "-Djava.class.path=C:\\Lib\\sextante_gui.jar"; 

转换为

  

java.exe -Djava.class.path = C:\ JavaCode; C:\ Lib \ sextante.jar; C:\ Lib \ sextante_gui.jar