Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

Fareed karozrk Mortuza




Posted Questions



Wait...

Posted Answers



Answer


Java is a platform independent programming language. An application written in Java does not require recompilation for running in different platforms. Java compiler never compiles the code to native platform instructions binaries which is often seen with C, C++ programming languages. Java compiler (JAVAC) takes .java source files and compiles them into object files. These object files are in the form of JAVA byte code. To run these object codes JAVA setup provides JAVA runtime Environment (JRE). JAVA runtime Environment consists of a program running tool as JAVA and a couple of libraries and run time binaries. These are platform dependent. JAVA makes JRE for each platforms. JRE for Windows, Linux, Android are different kind of binaries and they are native to platfrom architectures like x86, x86_64, ARM, ARM64 etc. JAVA byte codes are interpreted by the runtime binary to translate to native instructions and then they run as native code.

JAVA is good for writing codes without much knowledge of the platform on which it will be running. However system applications may need to access some of the system resources which are strictly platform dependent. C, C++ are the languages which can access OS resources and low level interfaces. Java cannot access this directly from java context. However there is a provision in Java to interface with native binaries. This interface is well known as Java Native Interface or JNI in short. JNI in the gateway between JAVA and native binaries, written in C/C++/VB or any other compiled languages. Most of the system applications are developed in two parts. One part which is tightly coupled to OS and low layer calls are written with C,C++. These binaries exposes native calls to JAVA environment. The other halves of the JAVA code calls these native interfaces.

Above code has three functions which are declared with native keyword. Native keyword informs Java Compiler that the function can be linked even if the definition of HelloWorld(), CallC() and SetMembers() do not exist. This is similar to importing a function from DLL/SO file. Java compiler puts these interfaces in import table or checks import table of DLL/so during runtime linking.

At this point if we try to execute this code we will get error as Java runtime will fail to load import library MyJNI.dll or libMyJNI.so.

So the java side of the application is ready but the native implementation part is missing here. Let us see what are the things we need here.

Native DLL binary will be generated by compiling and linking the header and source files. We will see how these three files will be generated in this tutorial.

We need a tool called JAVAH which generates this interface. JAVAH comes with JDK and no additional installation or setup is required. Javah can be executed in the same way as we execute comiler javac or java. Javah takes byte code file name (without .class extension) as argument. It process than binary and generates a C/C++ header file in the same name.

Note carefully javah has generated three function prototype in C/C++. JNIEXPORT macro tells C/C++ compiler to export these functions from DLL/SO library so that java run time can import these calls. Additionally there are two extra arguments for each functions. First argument is of type JNIEnv which gives a pointer to Java Runtime Environment. JNIEnv provides many interfaces which native binary can utilize. Next argument is of type jobject which represents the object itself or the "this" pointer. We can access members of the object through this arguments. Next arguments are same as declared in java. jboolean, jbyte, jchar, jshort, jlong, jfloat, jdouble are type definition of unsigned char, signed char, signed short, short, int, long, float and double in C/C++. Now java treats strings as UNICODE thus jstring is not directly char * but conversions are possible in both the ways.

Call by reference in java is not possible but this is equivalent of Call by reference in java. Java_MyJNI_SetMembers access and modify member variables of MyJNI class object and thus can be treated as the equivalent of call by reference.

Now we will implement these interfaces in a dynamic link library in Windows or in a shared library in Linux. We need to create a C/C++ source file and may need to copy this header file to the same folder. Here we created MyJNI.c in the same folder where MyJNI.h is located. We should include this file and implement the definitions.

jchar, jshort, jlong, jfloat, jdouble are type definition of char, short, long, float and double in C/C++ thus we can used these directly. But this is not true for the strings. Java treats strings char char as UNICODE. There are two bytes for each characters. First character contains ASCII value and next one contains language id. jstring can be converted to UTF char* and vice versa. GetStringUTFChars() can be used to extract char* from Java strings. NewStringUTF() is used to allocate a new UNICODE string from UTF char*. ReleaseStringUTFChars() is used to release existing buffer.

Here in Java_MyJNI_CallC(), we are simply printing the values passed from Java in C/C++ context and directly returning to Java context. Java_MyJNI_SetMembers() is not taking arguments. Every C++/Java function has inherent member called this pointer. This function uses this object to access all member variables. JNI can access and modify all the members. These are the steps to access/modify Java members.

This is only a demo however in practical situation where system applications need to access system resources can open device files and can call mmap/ioctls/sysctl etc to interface deeper inside into system side.

Now we need to place this native library in the same folder where our Java Class binary located and can run. If we have more than one class We can also place this in the Java Runtime binary folder jre\bin if we want to access this from more than one java class located in different folder.

About our authors: Team EQA

You have viewed 1 page out of 27. Your DLL learning is 0.00% complete. Login to check your learning progress.


Answer is posted for the following question.

How to call cpp function from java?

Answer


City of Gold Coast


Answer is posted for the following question.

Where could I locate best places to live in Gold Coast, Australia australia?

Answer


The vet may advise you to feed your dog some home-cooked food like skinless chicken, boiled potatoes, and rice . These can be good short term-options for a dog with an upset stomach, but they are not nutritionally complete for long-term care. Take care of your dog as you would a sick child.


Answer is posted for the following question.

How to cure dog vomiting?

Answer


If you want to leave a 20% tip, multiply the cost by 0.20 to get the tip amount or multiply the cost by 1.20 to get the total including tip. If you want to leave a 18% tip, multiply the cost by 0.18 to get the tip amount or multiply the cost by 1.18 to get the total including tip.


Answer is posted for the following question.

How to find percentage tip?

Answer


  1. Do aerobic exercise The first step to burning overall body fat is aerobic exercise
  2. Strengthen your muscles Losing fat alone can leave you with less toned legs, so you'll need to invest some time in strengthening your muscles
  3. Reduce your calorie intake

Answer is posted for the following question.

How to lose weight in your legs?

Answer


This site is designed, developed and hosted by National Informatics Centre, Ministry of Electronics & Information Technology, Government of India


Answer is posted for the following question.

How many ration card holders in india?

Answer


A California Corporation Here at 5X Racing we are Spec Miata Other approved parts Techno Toy Tuning knows that you will use our


Answer is posted for the following question.

Would you suggest best evo tuner in California?

Answer


Best Places To Live In North Las Vegas, NV · Green Valley North, Henderson, NV · Neighborhood: 9.8mi / 15.8km away · Population: 39,727 · This neighborhood, Green


Answer is posted for the following question.

What is the best neighborhoods in north las vegas?

Answer


1
#include
2
string s;
3
cin>>s;
4
int len;
5
len=s.length();

Answer is posted for the following question.

How to strring length in c++ (C++ Programming Language)


Wait...