JNI test connection with spring

This commit is contained in:
MathewFrancis 2025-08-17 11:03:31 +05:30
parent 213945b2e9
commit b697e66b2c
7 changed files with 66 additions and 0 deletions

8
JNI/build.sh Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -e
gcc -fPIC -shared \
-I"$JAVA_HOME/include" \
-I"$JAVA_HOME/include/linux" \
-o libbtcjni.so native_impl.c
echo "Built libbtcjni.so"

BIN
JNI/libbtcjni.so Executable file

Binary file not shown.

28
JNI/native_impl.c Normal file
View File

@ -0,0 +1,28 @@
//native_impl.c
#include <jni.h>
#include <stdio.h>
#include <string.h>
// JNI functions for com.example.cezenBTC.service.NativeBridge
JNIEXPORT jstring JNICALL
Java_com_example_cezenBTC_service_NativeBridge_hello(JNIEnv *env, jclass clazz, jstring jname) {
(void)clazz; // unused
const char *name = jname ? (*env)->GetStringUTFChars(env, jname, NULL) : "World";
char buf[256];
snprintf(buf, sizeof(buf), "Hello from JNI, %s!", (name && *name) ? name : "World");
if (jname) {
(*env)->ReleaseStringUTFChars(env, jname, name);
}
return (*env)->NewStringUTF(env, buf);
}
JNIEXPORT jint JNICALL
Java_com_example_cezenBTC_service_NativeBridge_add(JNIEnv *env, jclass clazz, jint a, jint b) {
(void)env; (void)clazz;
return a + b;
}

View File

@ -109,6 +109,7 @@ public class CezenRoutsSecurityChain {
"/btc/get_races_by_venue_id", "/btc/get_races_by_venue_id",
"/btc/get_all_races_by_today", "/btc/get_all_races_by_today",
"/btc/get_race_card", "/btc/get_race_card",
"/btc/tester",
"/cezen/set_aors", "/cezen/set_aors",
"/cezen/set_password", "/cezen/set_password",
"/cezen/add_extension" "/cezen/add_extension"

View File

@ -47,4 +47,10 @@ public class BtcOperations {
return this.btcRaceService.getRaceCard(LocalDate.now()); return this.btcRaceService.getRaceCard(LocalDate.now());
} }
@GetMapping("/tester")
public ResponseEntity<String> testData(){
return this.btcRaceService.testConnection();
}
} }

View File

@ -114,4 +114,13 @@ public class BtcRaceService {
return ResponseEntity.status(200).body(raceCard); return ResponseEntity.status(200).body(raceCard);
} }
// this is a JNI test to spring
public ResponseEntity<String> testConnection(){
//jni logic here
String msg = NativeBridge.hello("Spring");
int sum = NativeBridge.add(21, 21);
return ResponseEntity.ok(msg + " | add(21,21)=" + sum);
}
} }

View File

@ -0,0 +1,14 @@
package com.example.cezenBTC.service;
public final class NativeBridge {
public static native String hello(String name);
public static native int add(int a, int b);
static {
// ABSOLUTE PATH to the generated library
// Linux: libbtcjni.so, macOS: libbtcjni.dylib, Windows: btcjni.dll
System.load("/home/mathew/Desktop/dev/horse/JNI_spring/btc_horse/JNI/libbtcjni.so"); // <-- change this to the real path
}
private NativeBridge() {}
}