JAVA-26394 Move java-native to core-java modules (#15092)

This commit is contained in:
anuragkumawat
2023-11-04 14:53:37 +05:30
committed by GitHub
parent 36a30dcda8
commit df78df7a81
38 changed files with 4 additions and 5 deletions
@@ -0,0 +1,48 @@
#include "com_baeldung_jni_ExampleObjectsJNI.h"
#include <iostream>
/*
* Class: com_baeldung_jni_ExampleObjectsJNI
* Method: createUser
* Signature: (Ljava/lang/String;D)Lcom/baeldung/jni/UserData;
*/
JNIEXPORT jobject JNICALL Java_com_baeldung_jni_ExampleObjectsJNI_createUser
(JNIEnv *env, jobject thisObject, jstring name, jdouble balance){
// Create the object of the class UserData
jclass userDataClass = env->FindClass("com/baeldung/jni/UserData");
jobject newUserData = env->AllocObject(userDataClass);
// Get UserData fields to set
jfieldID nameField = env->GetFieldID(userDataClass , "name", "Ljava/lang/String;");
jfieldID balanceField = env->GetFieldID(userDataClass , "balance", "D");
// Set the values of the new object
env->SetObjectField(newUserData, nameField, name);
env->SetDoubleField(newUserData, balanceField, balance);
// Return the created object
return newUserData;
}
/*
* Class: com_baeldung_jni_ExampleObjectsJNI
* Method: printUserData
* Signature: (Lcom/baeldung/jni/UserData;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_baeldung_jni_ExampleObjectsJNI_printUserData
(JNIEnv *env, jobject thisObject, jobject userData){
// Find the class method id
jclass userDataClass = env->GetObjectClass(userData);
jmethodID methodId = env->GetMethodID(userDataClass, "getUserInfo", "()Ljava/lang/String;");
// Call the object method and get the result
jstring result = (jstring)env->CallObjectMethod(userData, methodId);
// Print the result
std::cout << "C++: User data is: " << env->GetStringUTFChars(result, NULL) << std::endl;
return result;
}
@@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_baeldung_jni_ExampleObjectsJNI */
#ifndef _Included_com_baeldung_jni_ExampleObjectsJNI
#define _Included_com_baeldung_jni_ExampleObjectsJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_baeldung_jni_ExampleObjectsJNI
* Method: createUser
* Signature: (Ljava/lang/String;D)Lcom/baeldung/jni/UserData;
*/
JNIEXPORT jobject JNICALL Java_com_baeldung_jni_ExampleObjectsJNI_createUser
(JNIEnv *, jobject, jstring, jdouble);
/*
* Class: com_baeldung_jni_ExampleObjectsJNI
* Method: printUserData
* Signature: (Lcom/baeldung/jni/UserData;)V
*/
JNIEXPORT jstring JNICALL Java_com_baeldung_jni_ExampleObjectsJNI_printUserData
(JNIEnv *, jobject, jobject);
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,34 @@
#include "com_baeldung_jni_ExampleParametersJNI.h"
#include <iostream>
#include <string>
/*
* Class: com_baeldung_jni_ExampleParametersJNI
* Method: sumIntegers
* Signature: (II)J
*/
JNIEXPORT jlong JNICALL Java_com_baeldung_jni_ExampleParametersJNI_sumIntegers (JNIEnv* env, jobject thisObject, jint first, jint second){
std::cout << "C++: The numbers received are : " << first << " and " << second << std::endl;
return (long)first + (long)second;
}
/*
* Class: com_baeldung_jni_ExampleParametersJNI
* Method: sayHelloToMe
* Signature: (Ljava/lang/String;Z)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_baeldung_jni_ExampleParametersJNI_sayHelloToMe (JNIEnv* env, jobject thisObject, jstring name, jboolean isFemale){
const char* nameCharPointer = env->GetStringUTFChars(name, NULL);
std::cout << "C++: The string received is: " << nameCharPointer << std::endl;
std::string title;
if(isFemale){
title = "Ms. ";
}
else{
title = "Mr. ";
}
std::string fullName = title + nameCharPointer;
return env->NewStringUTF(fullName.c_str());
}
@@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_baeldung_jni_ExampleParametersJNI */
#ifndef _Included_com_baeldung_jni_ExampleParametersJNI
#define _Included_com_baeldung_jni_ExampleParametersJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_baeldung_jni_ExampleParametersJNI
* Method: sumIntegers
* Signature: (II)J
*/
JNIEXPORT jlong JNICALL Java_com_baeldung_jni_ExampleParametersJNI_sumIntegers
(JNIEnv*, jobject, jint, jint);
/*
* Class: com_baeldung_jni_ExampleParametersJNI
* Method: sayHelloToMe
* Signature: (Ljava/lang/String;Z)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_baeldung_jni_ExampleParametersJNI_sayHelloToMe
(JNIEnv*, jobject, jstring, jboolean);
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,13 @@
#include "com_baeldung_jni_HelloWorldJNI.h"
#include <iostream>
/*
* Class: com_baeldung_jni_HelloWorldJNI
* Method: sayHello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_baeldung_jni_HelloWorldJNI_sayHello (JNIEnv* env, jobject thisObject) {
std::string hello = "Hello from C++ !!";
std::cout << hello << std::endl;
return env->NewStringUTF(hello.c_str());
}
@@ -0,0 +1,21 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_baeldung_jni_HelloWorldJNI */
#ifndef _Included_com_baeldung_jni_HelloWorldJNI
#define _Included_com_baeldung_jni_HelloWorldJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_baeldung_jni_HelloWorldJNI
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT jstring JNICALL Java_com_baeldung_jni_HelloWorldJNI_sayHello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,21 @@
#include "com_baeldung_jni_RegisterNativesHelloWorldJNI.h"
#include <iostream>
JNIEXPORT jstring JNICALL hello (JNIEnv* env, jobject thisObject) {
std::string hello = "Hello from registered native C++ !!";
std::cout << hello << std::endl;
return env->NewStringUTF(hello.c_str());
}
static JNINativeMethod methods[] = {
{"sayHello", "()Ljava/lang/String;", (void*) &hello },
};
JNIEXPORT void JNICALL Java_com_baeldung_jni_RegisterNativesHelloWorldJNI_register (JNIEnv* env, jobject thsObject) {
jclass clazz = env->FindClass("com/baeldung/jni/RegisterNativesHelloWorldJNI");
(env)->RegisterNatives(clazz, methods, sizeof(methods)/sizeof(methods[0]));
}
@@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_baeldung_jni_RegisterNativesHelloWorldJNI */
#ifndef _Included_com_baeldung_jni_RegisterNativesHelloWorldJNI
#define _Included_com_baeldung_jni_RegisterNativesHelloWorldJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_baeldung_jni_RegisterNativesHelloWorldJNI
* Method: sayHello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_baeldung_jni_RegisterNativesHelloWorldJNI_sayHello
(JNIEnv *, jobject);
/*
* Class: com_baeldung_jni_RegisterNativesHelloWorldJNI
* Method: register
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_baeldung_jni_RegisterNativesHelloWorldJNI_register
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,6 @@
REM Create the header with javac -h . ClassName.java
REM Remember to set your JAVA_HOME env var
g++ -c -I%JAVA_HOME%\include -I%JAVA_HOME%\include\win32 com_baeldung_jni_HelloWorldJNI.cpp -o com_baeldung_jni_HelloWorldJNI.o
g++ -c -I%JAVA_HOME%\include -I%JAVA_HOME%\include\win32 com_baeldung_jni_ExampleParametersJNI.cpp -o com_baeldung_jni_ExampleParametersJNI.o
g++ -c -I%JAVA_HOME%\include -I%JAVA_HOME%\include\win32 com_baeldung_jni_ExampleObjectsJNI.cpp -o com_baeldung_jni_ExampleObjectsJNI.o
g++ -shared -o ..\..\..\native\win32\native.dll com_baeldung_jni_HelloWorldJNI.o com_baeldung_jni_ExampleParametersJNI.o com_baeldung_jni_ExampleObjectsJNI.o -Wl,--add-stdcall-alias
@@ -0,0 +1,7 @@
# Create the header with javac -h . ClassName.java
# Remember to set your JAVA_HOME env var
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux com_baeldung_jni_HelloWorldJNI.cpp -o com_baeldung_jni_HelloWorldJNI.o
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux com_baeldung_jni_ExampleParametersJNI.cpp -o com_baeldung_jni_ExampleParametersJNI.o
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux com_baeldung_jni_ExampleObjectsJNI.cpp -o com_baeldung_jni_ExampleObjectsJNI.o
g++ -shared -fPIC -o ../../../native/linux_x86_64/libnative.so com_baeldung_jni_HelloWorldJNI.o com_baeldung_jni_ExampleParametersJNI.o com_baeldung_jni_ExampleObjectsJNI.o -lc
# Don't forget to set java.library.path to point to the folder where you have the libnative you're loading.
@@ -0,0 +1,7 @@
# Create the header with javac -h . ClassName.java
# Remember to set your JAVA_HOME env var
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_HelloWorldJNI.cpp -o com_baeldung_jni_HelloWorldJNI.o
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_ExampleParametersJNI.cpp -o com_baeldung_jni_ExampleParametersJNI.o
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_ExampleObjectsJNI.cpp -o com_baeldung_jni_ExampleObjectsJNI.o
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp -o com_baeldung_jni_RegisterNativesHelloWorldJNI.o
g++ -dynamiclib -o ../../../native/macos/libnative.dylib com_baeldung_jni_HelloWorldJNI.o com_baeldung_jni_ExampleParametersJNI.o com_baeldung_jni_ExampleObjectsJNI.o com_baeldung_jni_RegisterNativesHelloWorldJNI.o -lc
@@ -0,0 +1,13 @@
#include "com_baeldung_unsatisfiedlink_JniUnsatisfiedLink.h"
#include <iostream>
/*
* Class: com_baeldung_unsatisfiedlink_JniUnsatisfiedLink
* Method: test
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_baeldung_unsatisfiedlink_JniUnsatisfiedLink_test (JNIEnv* env, jobject thisObject) {
std::string test = "Test OK";
std::cout << test << std::endl;
return env->NewStringUTF(test.c_str());
}
@@ -0,0 +1,21 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_baeldung_unsatisfiedlink_JniUnsatisfiedLink */
#ifndef _Included_com_baeldung_unsatisfiedlink_JniUnsatisfiedLink
#define _Included_com_baeldung_unsatisfiedlink_JniUnsatisfiedLink
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_baeldung_unsatisfiedlink_JniUnsatisfiedLink
* Method: test
* Signature: ()V
*/
JNIEXPORT jstring JNICALL Java_com_baeldung_unsatisfiedlink_JniUnsatisfiedLink_test
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,22 @@
#!/bin/bash
# Create the header with javac -h . ClassName.java
# Remember to set your JAVA_HOME env var
# Don't forget to set java.library.path to point to the folder where you have the lib you're loading.
MYSELF="$(readlink -f "$0")"
MYDIR="${MYSELF%/*}"
cd "$MYDIR"
class_name=com_baeldung_unsatisfiedlink_JniUnsatisfiedLink
lib_name=test
g++ -c -fPIC -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" ${class_name}.cpp -o ${class_name}.o
g++ -shared -fPIC -o lib${lib_name}.so ${class_name}.o -lc
# 32-bit version
g++ -m32 -c -fPIC -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" ${class_name}.cpp -o ${class_name}32.o
g++ -m32 -shared -fPIC -o lib${lib_name}32.so ${class_name}32.o -lc
# dummy version
touch ${class_name}-dummy.o
ls lib*