简体   繁体   English

如何从 Java 调用 C++?

[英]How to call C++ from Java?

I wanted to call a C++ method from Java.我想从 Java 调用 C++ 方法。 I read about JNI, but I am not getting how to get all the library files and where I should keep it in order to run the program from command line.我读过 JNI,但我不知道如何获取所有库文件以及我应该在哪里保存它以便从命令行运行程序。

Is there any way to call a C++ method from Eclipse itself, because I am using it to run Java classes.有什么方法可以从 Eclipse 本身调用 C++ 方法,因为我使用它来运行 Java 类。

While I've used JNI-C++ bridging in the past (only a little though) - it can be a bit ugly.虽然我过去使用过 JNI-C++ 桥接(虽然只是一点点) - 它可能有点难看。 You might want to consider using SWIG to help you generate all the messy boiler plate code.您可能需要考虑使用SWIG来帮助您生成所有杂乱的样板代码。

If JNI is too complicated you can take a look at JNA .如果 JNI 太复杂,你可以看看JNA In first case you have to create native wrapper code (in C or C++) to join Java and native (C++/C/...) code.在第一种情况下,您必须创建本机包装器代码(在 C 或 C++ 中)以连接 Java 和本机 (C++/C/...) 代码。 In second case it is done at runtime (so you only need Java code + config).在第二种情况下,它是在运行时完成的(因此您只需要 Java 代码 + 配置)。

I use JNR/FFI https://github.com/jnr/jnr-ffi which I have found to be faster than JNA and easier than SWIG.我使用 JNR/FFI https://github.com/jnr/jnr-ffi ,我发现它比 JNA 更快,比 SWIG 更容易。 It's as close to JNI for speed I have found but less error prone.我发现它的速度与 JNI 非常接近,但更不容易出错。

I have used JNA before for simple interfaces and it was simple and elegant enough.我之前使用过 JNA 来处理简单的接口,它足够简单和优雅。 It is advised though, that if there is a complex interface then it's better to use SWIG.但建议,如果有一个复杂的界面,那么最好使用 SWIG。

There are some good answers that compare SWIG with JNI and JNA.有一些很好的答案可以将 SWIG 与 JNI 和 JNA 进行比较。 It's a while since the question was asked though.自从提出这个问题以来已经有一段时间了。

SWIG vs JNI and JNA SWIG 与 JNI 和 JNA

JavaCPP - https://github.com/bytedeco/javacpp JavaCPP - https://github.com/bytedeco/javacpp

It provides efficient access to native C++ inside Java.它提供了对 Java 内本机 C++ 的高效访问。 Under the hood, it uses JNI, so it works with all implementations of Java SE.在底层,它使用 JNI,因此它适用于 Java SE 的所有实现。

Much easier than JNA/JNI比 JNA/JNI 容易得多

Call c++ code from java program.从java程序调用c++代码。

Follow the below Step按照下面的步骤

  1. write java code编写java代码
    • that contian declaration of native methods本地方法的 contian 声明
    • load shared library contain native code加载共享库包含本机代码
    • call native method调用本地方法
public class Sample1 
{
     public native int intMethod(int n);
     public static void main(String[] args)
     {
          System.loadLibrary("Sample1");
          Sample1 sample = new Sample1();
          int     square = sample.intMethod(5);
          System.out.println("intMethod: " + square);
     }
}
  1. Compile java code编译java代码

javac Sample2.java

  1. create c++ header file创建 C++ 头文件

javac Sample2.java

  1. Write c++ code编写 C++ 代码
#include "Sample1.h"
#include <string.h>

JNIEXPORT jint JNICALL Java_Sample1_intMethod
  (JNIEnv *env, jobject obj, jint num) {
   return num * num;
}

void main(){}
  1. compile c++ code编译 C++ 代码

cc -G Sample1.c -o Sample1.so

  1. Run java program运行java程序

java Sample1

JNA can be used instead of JNI .可以使用JNA代替 JNI 。

All you need is to download JNA jar ( https://github.com/java-native-access/jna#download ) Which should be included in your java project.您只需要下载JNA jar ( https://github.com/java-native-access/jna#download ),它应该包含在您的 java 项目中。

You need to give the location of your c++ library in your project properties.您需要在项目属性中提供 C++ 库的位置。

  • Right click project右键单击项目
  • Run
  • In VM options include this -Djava.library.path="C:Your dll location" .在 VM 选项中包括此-Djava.library.path="C:Your dll location"

Visit this site for an example which also has a source code for Java and c++ project to know how this works.访问此站点以获取示例,其中还包含 Java 和 C++ 项目的源代码,以了解其工作原理。 ( http://blog.mwrobel.eu/how-to-call-dll-methods-from-java/#a_downloads ) http://blog.mwrobel.eu/how-to-call-dll-methods-from-java/#a_downloads

How to call a C/C++ function from Java then you can use Java Native Interface (JNI), part of the Java platform, is an interface that enables communication between Java applications running on a Java Virtual Machine (JVM) and native applications or libraries written in other programming languages (eg C, C++).如何从 Java 调用 C/C++ 函数,然后您可以使用 Java 本机接口 (JNI),它是 Java 平台的一部分,是一个接口,可以在 Java 虚拟机 (JVM) 上运行的 Java 应用程序与本机应用程序或库之间进行通信用其他编程语言(例如 C、C++)编写。 You can use some below URLs for as examples.您可以使用以下一些 URL 作为示例。

http://malinsky.eu/blog/how-to-call-ac-function-from-java/ https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html http://malinsky.eu/blog/how-to-call-ac-function-from-java/ https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

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

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