Sunday, August 7, 2011

C Call Java function under Linux/Windows

[1] Ref from codeproject



[q.yang@localhost CTest]$ ll
total 16
-rwxr--r-- 1 q.yang developer 7298 2011-08-05 15:03 CTest.cpp
drwxr-xr-x 2 q.yang developer 4096 2011-08-08 11:31 debug
-rwxr--r-- 1 q.yang developer 838 2011-08-08 11:32 makefile
[q.yang@localhost CTest]$ echo $JAVA_HOME
/usr/java/default
[q.yang@localhost CTest]$ pwd
/home/q.yang/EclipseCDT/qywkspace/Sandbox/Sample_017_CCallJava_via_JNI/CTest
[q.yang@localhost CTest]$ cat makefile
DESTDIR = ./debug
PROGRAM = $(DESTDIR)/CCallJava
SRCDIR = ./
CC = g++
OBJS := $(addprefix $(DESTDIR)/,CTest.o)
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG) -D GSN_HOST_NOT_SUPPORT_RD_CONFIG
INCL = -I ./include -I $(JAVA_HOME)/include -I $(JAVA_HOME)/include/linux
LIB_FLAGS = -ljvm
LD_FLAGS = -L $(JAVA_HOME)/jre/lib/i386/server/ -L $(JAVA_HOME)/jre/lib/i386/
#-----------------------------------------------
#Compile and Link Image
#------------------------------------------------
all: $(PROGRAM)
$(PROGRAM): $(OBJS)
$(CC) -o $(PROGRAM) $(OBJS) -pthread $(LD_FLAGS) $(LIB_FLAGS)
$(DESTDIR)/%.o:$(SRCDIR)/%.cpp
$(CC) -c $(CFLAGS) $< -o $@ $(INCL)
#-----------------------------------------------
#Clean obj and image files.
#------------------------------------------------
clean :
rm -f $(OBJS)
rm -f $(PROGRAM)



COMPILE ALL JAVA PROGRAM
------------------------------
[q.yang@localhost TestStruct]$ javac *.java


RUN C PROGRAM TO CALL JAVA FUNCTION
------------------------------------
[q.yang@localhost Sandbox]$ tree Sample_017_CCallJava_via_JNI/
Sample_017_CCallJava_via_JNI/
|-- CTest
| |-- CTest.cpp
| |-- debug
| | |-- CCallJava
| | `-- CTest.o
| `-- makefile
|-- JavaSrc
| `-- TestStruct
| |-- ControlDetail.class
| |-- ControlDetail.java
| |-- HelloWorld.class
| |-- HelloWorld.java
| |-- ReturnData.class
| |-- ReturnData.java
| |-- WorkOrder.class
| `-- WorkOrder.java
`-- readme.txt


[q.yang@localhost CTest]$ export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/i386/server/:$JAVA_HOME/jre/lib/i386/
[q.yang@localhost CTest]$ echo $LD_LIBRARY_PATH
/usr/java/default/jre/lib/i386/server/:/usr/java/default/jre/lib/i386/

[q.yang@localhost debug]$ ./CCallJava
Struct Created in C has values:
ID:11
Name:HR-HW
IP:10.32.164.133
Port:9099
Hello World!
This is the main function in HelloWorld class

TestCall:Called from the C Program


Going to Call DisplayStruct
Structure is:
-------------------------
Name:HR-HW
IP:10.32.164.133
Port9099


Going to call DisplayStructArray From C

WorkOrders are Given hereunder:
----------------------------
<---Work Order Number:1<---
Sum_Serial_ID: 2000
Access_Number: 2878430
Action_Type: 04
Effective_Date: 25-12-2007 12:20:30 PM
Fetch_Flag: 0
Reason: Executed Successfully
Access_Source: PMS
<---Work Order Number:2<---
Sum_Serial_ID: 1000
Access_Number: 2878000
Action_Type: T4
Effective_Date: 25-12-2007 11:20:30 PM
Fetch_Flag: 0
Reason:
Access_Source: RMS
Going to return an object from java


Values Returned from Object are:
returnValue=1
Log=Successfull function call
[q.yang@localhost debug]$

No comments: