41

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.

jonsca
  • 10,218
  • 26
  • 54
  • 62
user970500
  • 571
  • 1
  • 7
  • 14

8 Answers8

9

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.

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
7

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).

TwentyCharMax
  • 123
  • 12
Fernando Miguélez
  • 11,196
  • 6
  • 36
  • 54
  • "JNI" is the standard answer. But if JNA meets your needs - cool! @Fernando Miguelez: Great suggestion! Thank you for sharing! Here's another link: http://www.javaworld.com/javaworld/jw-02-2008/jw-02-opensourcejava-jna.html – paulsm4 Sep 29 '11 at 06:47
5

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    BTW, JavaCPP is just as fast as JNR. I even stumbled across a performance regression in JDK 11 when running some benchmarks against Panama with it: https://bugs.openjdk.java.net/browse/JDK-8210975 – Samuel Audet Apr 04 '19 at 01:41
4

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

Arabasta
  • 805
  • 2
  • 7
  • 13
4

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

Ariel
  • 327
  • 2
  • 6
4

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

Bharat Vankar
  • 317
  • 2
  • 13
1

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 )

0

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 (e.g. C, C++). You can use some below URLs for as examples.

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

Kröw
  • 504
  • 2
  • 13
  • 31
mkumar0304
  • 637
  • 7
  • 8