简体   繁体   English

使用jna从Java调用c ++ main方法

[英]invoke c++ main method from java using jna

I am trying to invoke a c++ method from java using JNA on a mac/unix environment. 我试图在mac / unix环境中使用JNA从Java调用c ++方法。

I have two questions. 我有两个问题。

  1. So far I could successfully implement simple examples but I am not sure how i could write a java counter part for the following C++ method: 到目前为止,我可以成功实现简单的示例,但是我不确定如何为以下C ++方法编写Java计数器部分:

     int main(int argc, char* argv[]){...} 

Java code I tried 我试过的Java代码

public interface CTest extends Library {
        int main(int argc, Pointer argv);
    }

Gives

java.lang.UnsatisfiedLinkError: Error looking up function 'main': dlsym(0x7fdee8c39300, main): symbol not found java.lang.UnsatisfiedLinkError:查找函数“ main”时出错:dlsym(0x7fdee8c39300,main):找不到符号

Also tried the following 还尝试了以下

public interface CTest extends Library {
        int main(int argc, Object... argv[]);
        //doesnt work either - int main(int argc, String argv);
    }

To no avail. 无济于事。 Same issue- symbol not found. 相同问题-找不到符号。 Any tips? 有小费吗? Ultimately I need to do something like this: 最终,我需要执行以下操作:

    CTest ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
    String obj[] = new String[]{"-v, filepath/file"};
    ctest.main(2, obj);
  1. The second question is regarding implementing the above in a multi-threaded environment. 第二个问题是有关在多线程环境中实现上述内容的问题。 I might have multiple libraries sitting in different locations so System.setProperty("jna.library.path",path-to-dylib) wont work. 我可能有多个位于不同位置的库,因此System.setProperty("jna.library.path",path-to-dylib)无法工作。 How can I achieve this? 我该如何实现?

Ok I figured this out myself, hoping for a quick answer but whatever. 好的,我自己弄清楚了,希望能有一个快速的答案,但无论如何。 Here is the solution: 解决方法如下:

Answer-1 : Below is my interface method. 答1:下面是我的界面方法。

    public interface CTest extends Library {
        public int main(int arc, String[] argv);
    }

Answer-2: This is how I am planning to handle multithreading 答案2:这就是我计划处理多线程的方式

    String argv[] = {"-v","path-to-file/file.extension"};
    System.load(this.getClass().getResource("libmylib.dylib").getFile());
    //load a different library later if required.. this way multiple threads load respective libs when/what they want
    HelloJNA.CTest ctest = (HelloJNA.CTest) Native.loadLibrary("mylib", HelloJNA.CTest.class);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM