OneCompiler

Dll

1627

Name: Sneha Bhandari

Roll no: 14

Java Class
public class MathOperations {
// Declare native methods
public native int add(int a, int b);
public native int subtract(int a, int b);

static {
    // Load the DLL
    System.loadLibrary("MathOperations");
}

public static void main(String[] args) {
    MathOperations mathOps = new MathOperations();
    System.out.println("Addition: " + mathOps.add(5, 3));
    System.out.println("Subtraction: " + mathOps.subtract(5, 3));
}

}

Header File
javac MathOperations.java
javah -jni MathOperations

Native Methods in C/C++
#include <jni.h>
#include "MathOperations.h"

JNIEXPORT jint JNICALL Java_MathOperations_add(JNIEnv *env, jobject obj, jint a, jint b) {
return a + b;
}

JNIEXPORT jint JNICALL Java_MathOperations_subtract(JNIEnv *env, jobject obj, jint a, jint b) {
return a - b;
}
C++ Code into a DLL:
gcc -shared -o libMathOperations.dll -I"%JAVA_HOME%/include" -I"%JAVA_HOME%/include/win32" MathOperations.c