0

Below is my scenario

I have to develop a java code that needs to communicate with C Library ( DNP3 Library ) , i assume below are the steps

  1. Understand the C code
  2. Write a JNI wrapper for C code
  3. Wite a Java program to call the JNI which in turn calls the C code

I have never worked on JNI , going through few examples i found they write the java code first and generate a .h file (JNI ) and then a corresponding C file.

Can you please reference me to any article that goes the other way round like C library (.c) -> JNI -> Java code ?

Thanks in advance Rakesh

Rakesh J R
  • 31
  • 5
  • 2
    The "corresponding C file" *is* your wrapper for the C library. That's the intended way of writing JNI, it's unclear what your intended creation flow is supposed to be like – UnholySheep Sep 10 '20 at 11:34
  • You should write a simple java file first, too. It doesn't have to contain anything but `native` method definitions that you want to use later. Then, as you found in the examples, you create the .h and now you can write the .c file with the actual wrapper functions – Ingo Leonhardt Sep 10 '20 at 11:35
  • @UnholySheep I have a c library ( DNP3 by TMW ) and i use its functionalities in my Java module so was trying to explore ways to access those. – Rakesh J R Sep 10 '20 at 16:58
  • @Ingo i have a c library already so is there a way to generate a .h and java file? like a reverse flow – Rakesh J R Sep 10 '20 at 16:58
  • 1
    Yes, and the workflow you found in the tutorials is exactly how you would go about it. I'm unclear what you are trying to "reverse" here. You need to create the Java and the JNI connection code anyway, so whatever you are trying to reverse will just end up coming to the same conclusion as if you had followed the tutorials in the first place – UnholySheep Sep 10 '20 at 17:18
  • @UnholySheep sorry if i am confusing you. In my case i already have a c library provided by TMW it is a DNP3 c Library , my goal is to access all the functionalities in this C library from a Java application. Examples that i saw for JNI say - write your java class , then generate a header and then write a c implementation , where as i already have c library so what approach do i take . i am new to JNI please bear with me. – Rakesh J R Sep 11 '20 at 05:00
  • 1
    I guess this one will be the closest to what you are looking for: http://jnicookbook.owsiak.org/recipe-No-021/ - `anotherFunction()` is the code that mimics your "already have a c library" code. While `Java_recipeNo021_HelloWorld_displayMessage` is a wrapper that can be called from Java, and then, run your code. – Oo.oO Sep 11 '20 at 07:12
  • 1
    In fact, this one might be even more suitable for you: http://jnicookbook.owsiak.org/recipe-No-023/ - as here, I am showing how to call code from shared library. – Oo.oO Sep 11 '20 at 07:21
  • I thing you've got a wrong idea somehow. What you have found in the examples (first create java file to define `native` methods, then create .h and write .c) is *exacly* what to do when you have an existing C library and need to create a JNI wrapper – Ingo Leonhardt Sep 11 '20 at 09:00
  • Thank you so much @Oo.oO that is exactly what i was looking for , this will solve my issue. – Rakesh J R Sep 12 '20 at 16:04
  • @IngoLeonhardt you are right - I need to follow all the steps as show in JNI examples. And the last bit is calling the C Library function in my .c Implementation just like this http://jnicookbook.owsiak.org/recipe-No-021/ Thank you all for the help. – Rakesh J R Sep 12 '20 at 16:06
  • @RakeshJR - Cool :) Have fun with JNI :) Oo.oO – Oo.oO Sep 12 '20 at 16:14

1 Answers1

1

Look at SWIG for a low level Java abstraction of your C library, then build a higher level Java library on top of that. This is a common approach when creating language bindings.

Botje
  • 26,269
  • 3
  • 31
  • 41