Showing posts with label Java. Show all posts

Difference between JDK, JRE and JVM


Another aspect worth mentioning :

jdk (java development kit)

you will need it for development purposes like the name suggest ,,

example : a software company will have jdk install in their computer because they will need to devlop new software which include compile and run their java programs as well.

so we can say that jdk = jre+ jvm .


jre (java run-time environment)

It;s needed to run java programs ,, you can't compile java programs with it .

Example : a regular computer user who wants to is run some online games then will need jre in his system to run java programs.

jvm (java virtual machine)

As you might know it run the bytecodes . It make java platform independent because it executes the .class file which you get after you compile the java program no matter if you compile it on windows,mac or linux.

open jdk

well like i said above .... now jdk is made by different company ,,, one of them which happens to be an open source and free for public use is openjdk... while some others are Oracle Corporation's JRockit JDK or ibm jdk...

However they all might appear same to general user..

Conclusion

If you are a java program you will need jdk in your system and this package will include jre and jvm as well but if you are normal user who like to play online games then you will only need jre and this package will not have jdk in it.
In other words jdk is grandfather jre is father and jvm is their son.

How to call a C program from Java?


Calling a C program may be useful when we prefer to use C libraries and to reuse an existing C program. When we compile a C program, the source gets converted to obj file. It is a platform dependent intermediate machine code which will be converted to exe and then executed.
Java native interface (JNI) is a framework provided by java that enables java programs to call native code and vice-versa.

Using JNI a java program has the capability to call the native C code. But we lose the core objective of java which is platform independence. So calling a C program from java should be used judiciously.
JNI provides the specification and native code should be written/ported accordingly. JDK vendor should provide the needed implementation for the JNI.

Steps to call a C program from Java

1. Write the Java Program and Compile
2. Generate header file from java class
3. Write the C Program
4. Generate Shared Library File
5. Run Java Program

1. Write the Java Program

public class JavaToC {
    public native void helloC();
    static {
        System.loadLibrary("HelloWorld");
    }
    public static void main(String[] args) {
        new JavaToC().helloC();
    }
}

Note two things in this program,

  1. Use of native keyword. This is a method declaration and it informs the java compiler that the implementation for this method is a native one. This method helloC() is present in C source file and that is what we are going to call.
  2. Loading the library HelloWorld using static keyword. This library file will be compiled out of the C source in the coming steps.

2. Generate header file from java class


  1. JDK provides a tool named javah which can be used to generate the header file.
  2. We will use the generated header file as include in C source.
  3. Remember to compile the java program before using javah

javah JavaToC
Following is the header file generated,


/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JavaToC */

#ifndef _Included_JavaToC
#define _Included_JavaToC
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     JavaToC
 * Method:    helloC
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_JavaToC_helloC
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif


3. Write the C Program

#include <stdio.h>
#include <jni.h>
#include "JavaToC.h"
JNIEXPORT void JNICALL Java_JavaToC_helloC(JNIEnv *env, jobject javaobj)
{
  printf("Hello World: From C");
  return;
}

  1. stdio.h is the standard C header file include.
  2. jni.h is the header file that provides the java to C mapping and it is available in JDK.
  3. JavaToC.h is the header file generated from the java source file.

4. Generate Shared Library File

  1. Now compile the above C source file. We need a C compiler and I have chosen Tiny C.
  2. Tiny C can be downloaded from http://mirror.veriportal.com/savannah//tinycc/tcc-0.9.25-win32-bin.zip
  3. Download and unzip the file and set OS path to tcc.exe
  4. tcc HelloWorld.C -I C:/Progra~1/Java/jdk1.7.0_07/include -I C:/Progra~1/Java/jdk1.7.0_07/include/win32 -shared -o HelloWorld.dll
  5. Above is the command to generate the shared library file dll which is loaded in the java program. Two directories are included in the compile command, those are to include the jni.h and jni_md.h

5. Run Java Program

We are all set, now run the java program to get the following output,
Hello World: From C




Template by Clairvo Yance
Copyright © 2012 Enigma of IT and Blogger Themes.