简体   繁体   中英

How to call C++ from Java?

I wanted to call a C++ method from Java. 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.

Is there any way to call a C++ method from Eclipse itself, because I am using it to run Java classes.

While I've used JNI-C++ bridging in the past (only a little though) - it can be a bit ugly. You might want to consider using SWIG to help you generate all the messy boiler plate code.

If JNI is too complicated you can take a look at JNA . In first case you have to create native wrapper code (in C or C++) to join Java and native (C++/C/...) code. In second case it is done at runtime (so you only need Java code + config).

I use JNR/FFI https://github.com/jnr/jnr-ffi which I have found to be faster than JNA and easier than SWIG. It's as close to JNI for speed I have found but less error prone.

I have used JNA before for simple interfaces and it was simple and elegant enough. It is advised though, that if there is a complex interface then it's better to use SWIG.

There are some good answers that compare SWIG with JNI and JNA. It's a while since the question was asked though.

SWIG vs JNI and JNA

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

It provides efficient access to native C++ inside Java. Under the hood, it uses JNI, so it works with all implementations of Java SE.

Much easier than JNA/JNI

Call c++ code from java program.

Follow the below Step

  1. write java code
    • that contian declaration of native methods
    • 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

javac Sample2.java

  1. create c++ header file

javac Sample2.java

  1. Write c++ code
#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

cc -G Sample1.c -o Sample1.so

  1. Run java program

java Sample1

JNA can be used instead of 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.

You need to give the location of your c++ library in your project properties.

  • Right click project
  • Run
  • In VM options include this -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. ( 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++). You can use some below URLs for as examples.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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