Question
I facing fatal error: jni_md.h: No such file or directory #include jni_md.h on the code below Step 1 To create a JNI program that meets
I facing fatal error: jni_md.h: No such file or directory #include "jni_md.h" on the code below
Step 1
To create a JNI program that meets the requirements you specified where JNI helps to bridge between java and C++ code
CODE:
import java.util.Scanner;
public class Main{
static{
System.loadLibrary("native");
}
private native String[] fristNativeMethod(int number);
private native Srring[] secondNativeMethod(String[] countries);
public static void main(String[]args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int number = scanner.nextInt();
Main main = new Main();
String[] countries = main.firstNativeMethod(number);
String[] sortedCountries = main.secondNativeMethod(countries);
System.out.printIn("Sorted countries: ");
for (String country : sortedCountries){
System.out.printIn(country);
}
}
}
Step 2
This Java code prompts the user to enter the size of the array and then calls the fristNativeMethod with that size as an argument.It stores the returend array of country names in a variable called countries.It then calls the secondNativeMethod with the countries array as an argument, and stores the returned sorted array in a variable called sortedCountries.Finally, it prints out the sorted countries.
Here's the C++ code for the two native methods
CODE:
#include
#include
#include
#include
using namespace std;
extern "C" {
JNIEXPORT jobjectArray JNICALL Java_Main_firstNativeMethod(JNIEnv*env, jobject obj, jint
number){
jobjectArray result;
jclass stringClass = env->FindClass("java/lang/String");
result = env->NewObjectArray(number,stringClass,NULL);
for(int i = 0; i
cout
string country;
getline(cin,country);
jstring countryString = env->NewStringUTF(country.c_str());
env->SetObjectArrayElement(result, i, countryString);
}
return result;
}
Step 3
CODE:
JNIEXPORT jobjectArray JNICALL Java_Main_secondNativeMethodJNIEnv*env,jobject obj, jobjectArray countries){
int number = env->GetArrayLength(countries);
vector
for (int i = 0; i jstring country = (jstring) env->GetObjectArrayElement(countries,i); const char* countryCStr = env->GetStringUTFChars(country,NULL); v.push_back(countryCStr); env->ReleaseStringUTFChars(country,countryCStr); } sort(v.begin(),v.end()); jobjectArray result; jclass stringClass = env->FindClass("java/lang/String"); result = env->NewObjectArray(number,stringClass,NULL); for (int i = 0; i jstring countryString = env->NewStringUTF(v[i].c_str()); env->SetObjectArrayElement(result,i,countryString); } return result; } } This function takes in a jobjectArray called countries and converts it to a vector of strings.It then sorts the vector using the sort function from the algorithm library.Finally,it creates a new jobjectArray called result and populates it with the sorted list of countries by converting each string in the verctor back to a jstring and adding it to the result array. For output you may run this program by - gcc -I "%JAVA_HOME/include" -I "%JAVA_HOME/include/win32" -shared -o native.dll JNIExample.c Replace native.dll with the desired name of the native library, and JNIExample.c with the name of your C/C++file.you will also need to replace $JAVA_HOME with the path to your Java installation.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started