JNI with Java Azure Function

JES 0 Reputation points
2023-02-16T13:33:22.0366667+00:00

I have a bunch of C/C++ code which i want to expose as an azure function, i can see that its possible to create a custom handler and implement an http server which could receive the request, however this is a whole bunch of work i dont really need right now.

It occurred to me could I write a function "stub" in java and then call into to my native code using JNI from Java (i.e. native methods), i cant find anything on this and wondered if it was possible or supported.

cheers

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,570 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MughundhanRaveendran-MSFT 12,486 Reputation points
    2023-02-28T07:15:12.1533333+00:00

    @JES

    Yes, it is possible to call native code from Java using JNI (Java Native Interface). You can write a Java function that acts as a stub and then call into your native code using JNI. However, you need to make sure that the native code is compatible with the Azure Functions runtime environment.

     https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-java?tabs=bash%2Cconsumption

    Here is an example of how you can write a Java function that acts as a stub and calls into your native code using JNI:

    public class NativeFunction {
      public static native int nativeMethod(int x, int y);
    
      static {
        System.loadLibrary("NativeFunction");
      }
    
      public static int callNativeMethod(int x, int y) {
        return nativeMethod(x, y);
      }
    }
    
    

    You can then compile the Java code and generate a shared library from your native code. The shared library can be loaded by the Java code using the System.loadLibrary method.

    I hope this helps! Let me know if you have any other questions.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.