V. Srikanth
Posts: 9
Nickname: iamnobody
Registered: Dec, 2002
|
|
Re: How to do that ?Java application communicates with C application?
|
Posted: Dec 18, 2002 4:26 AM
|
|
Ever wonder on how to integrate JAVA and C programs ?? Ever had a question that "I have built a C library with my own reusable funtions and how do I use them in my Java application ??",
Following SIX STEPS demonstrate how to do that through a traditional Hello World program..
Have fun integrating your applications. -Srikanth.
Step 1: (Write the JAVA code )
###################################################### cat Main.java
class Main { public static void main(String[] args) { new HelloWorld().displayHelloWorld(); } }
class HelloWorld { public native void displayHelloWorld();
static { System.loadLibrary("hello"); } } ##########################################################
* Note the keyword "native" used for the method displayHelloWorld(). This indicate it's method defined in a native library and not in any other Java class. * Note the call to "System.loadlibrary" , this is the one which will load the "native library" in which displayHelloWorld is defined. (in case Unix system, System.loadLibrary("hello") will look for "libhello.so"
Step 2: (Compile the java code)
Using the command "javac Main.java"
This will generate two class files "Main.class" and "HelloWorld.class"
Step 3: (Create the .h file)
Using the command "javah HelloWorld"
This will generate header file HelloWorld.h which contain the Prototype of function to be put in the C file that we are going to write.
####################################################################### ############
cat HelloWorld.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class HelloWorld */
#ifndef _Included_HelloWorld #define _Included_HelloWorld #ifdef __cplusplus extern "C" { #endif /* * Class: HelloWorld * Method: displayHelloWorld * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject);
#ifdef __cplusplus } #endif #endif cat HelloWorld.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class HelloWorld */
#ifndef _Included_HelloWorld #define _Included_HelloWorld #ifdef __cplusplus extern "C" { #endif /* * Class: HelloWorld * Method: displayHelloWorld * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject);
#ifdef __cplusplus } #endif #endif
################################################## #####################
Step 4: (Write the native method implementation)
Write the C program which will implementation for JNICALL Java_HelloWorld_displayHelloWorld() function.
#################################################################### ################
cat HelloWorldImp.c #include "HelloWorld.h" #include <stdio.h>
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *myjni, jobject myobject) { printf("Hello World!\n"); return; }
#################################################################### #################
Step 5: (Create a shared library).
Standard way of creating a ".so" file in Unix. Using "cc -o libhello.so -G HelloWorldImp.c"
Step 6: (Run the program)
Now run the program as using "java Main"
You will be delighed to see the output "Hello World" on the screen !!!
Will this help Alex???
|
|