Use the same C Library in the board and in the Mock (simulation)

Greetings,

I would like to use a C library in Java to code a Mock (located in the platform frontpanel) for a foundation library.
The purpose is to use the same C library in the board and in the Mock during simulation.

Is there a way to do this?

Thanks in advance for your help,
Best regards.

Hi @s.connor
You can use C library in Java by using JNI (Java Native Interface).
You can load your C library from your Java code with the following line:

Runtime.getRuntime().load("clibrarypath/yourlibrary");

To call the C functions you have to declare native methods in your Java code, for example:

public native void yourMethod();

The name of the C function has to respect the pattern Java_classname_yourMethod with:

  • classname the name of the class in which the Java native method are declared
  • yourMethod the name of your native method

So with JNI the code you are calling must be made specifically for your application. You can create a C program that will expose the C library and that will be called by your Java appplication.

In order to generate the headers of your exposing C code you can use the tool javah that is in your JDK, it will generate the headers from your Java code: javah - C Header and Stub File Generator

You can find more information about JNI here: Java Native Interface Specification Contents
Hope this helps,
Alexis