→gedit DLL.java
import java.io.*;
import java.util.*;
class DLL{
static {
System.loadLibrary("DLL");
}
private native int add1(int a,int b);
private native int sub1(int a, int b);
private native int mult1(int a,int b);
private native int div1(int a,int b);
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int a,b,ch;
System.out.print("\\n Enter value of a: ");
a=sc.nextInt();
System.out.print("\\n Enter value of b: ");
b=sc.nextInt();
do
{
System.out.println("\\n Enter your choice: ");
ch=sc.nextInt();
switch(ch)
{
case 1:new DLL().add1(a,b);
break;
case 2:new DLL().sub1(a,b);
break;
case 3: new DLL().mult1(a,b);
break;
case 4: new DLL().div1(a,b);
break;
default:System.out.println("Your choice is wrong.");}
}while(ch<5);
}
}
→javac DLL.java
→gedit DLL.c
#include <jni.h>
#include <stdio.h>
#include "DLL.h"
JNIEXPORT jint JNICALL Java_DLL_add1(JNIEnv *env, jobject obj, jint a, jint b) {
int result = a + b;
printf("\\n%d + %d = %d\\n", a, b, result);
return result;
}
JNIEXPORT jint JNICALL Java_DLL_sub1(JNIEnv *env, jobject obj, jint a, jint b) {
int result = a - b;
printf("\\n%d - %d = %d\\n", a, b, result);
return result;
}
JNIEXPORT jint JNICALL Java_DLL_mult1(JNIEnv *env, jobject obj, jint a, jint b) {
int result = a * b;
printf("\\n%d * %d = %d\\n", a, b, result);
return result;
}
JNIEXPORT jint JNICALL Java_DLL_div1(JNIEnv *env, jobject obj, jint a, jint b) {
int result = (b != 0) ? (a / b) : 0;
printf("\\n%d / %d = %d\\n", a, b, result);
return result;
}
→javac DLL.java →javac -h . DLL.java →java -version //openjdk version "11.0.27" 2025-04-15 →gcc -shared -fPIC -I/usr/lib/jvm/java-11-openjdk-amd64/include -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux DLL.c -o libDLL.so
→java -Djava.library.path=. DLL



