Hello there, I would like an example of OpenMP with JNI implemented. It only nee
ID: 3567094 • Letter: H
Question
Hello there,
I would like an example of OpenMP with JNI implemented. It only needs print "Hello word", but my professor didn't explain or show us on how to do such thing. I have read information about it and only confuses me more. This is what he wrote on the board and as you can see if you click the link, he just change some wording... He only wants things done in Linux, so yeah... I currently installed Eclipse Luna Java EE, but I can't seem to find the libraries for c/c++ for the JNI... I would appreciate the help a lot. Thanks in advance!
Reference:
https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html
JNI class
Step 1:
public class JNItest {
static {
System.loadLibrary("testing jni");
}
// Declare a native method sayHello() that
receives nothing and returns void
private native void CHello();
// Test Driver
public static void main(String[] args) {
new JNItest().CHello(); // invoke the
native method
}
}
Step 2:
JNI test.c
#include <jni.h>
#include <stdio.h>
#include “JNItest”
JNIEXPORT void JNICALL
Java_JNItest_CHello(JNIEnv *env, jobjec thisobj){
printf(“Hello from C to java”);
return;
}
Modified in order to pass a string:
JNI test.c
#include <jni.h>
#include <stdio.h>
#include “JNItest”
JNIEXPORT void JNICALL
Java_JNItest_CHello(JNIENV *env, jobjec thisobj, jstring inJNISTR){
Const char int str =(*env)->GetStringUTFShare(env,inthisSTR,NULL);
printf(“received % ”, inc strr);
(*env)->Reload StringVTF(env, inJNISTR, incstr);
char message[600] =”your message here”;
return(*env)->NewStringVTF(env,message);
}
Modified in another way
Step 3:
compile:
You can also compile and link in two steps:
// Compile-only with -c flag. Output is
HElloJNI.o
> gcc -c -I"%JAVA_HOME%include"
-shared -o Hello.so
JNItest.c
Shared object-
Step 4:
Java -D ./hello.so JNI
Explanation / Answer
Here is the solution:
First create a class file HelloTest.
Execute it.
Now, in the menu bar or Eclipse, go to Run->External Tools->External Tools configuration.
Under “Program” folder select the “New Program”.
Then set the Name of the folder.
Now, under Main tab, set the values of Location, workspace, Arguments.
To set the Location, select the javah.exe path from the system.
Like: C:Program FilesJavajdk1.7.0_05injavah.exe
To set the workspace, select the workspace location as
${workspace_loc:/Demo/bin}
To set the arguments, set the path as,
-jni -verbose -d "${project_loc:} ${system_property:file.separator}jni" ${java_type_name}
or
-jni –v –d ${project_loc:}/bin classname
Here, java_type_name should be fully qualified class file name.
Then press “Apply” followed by “Run”.
Then once again refresh or press run button on the eclipse.
Hope this should be help full.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.