JAVA JNI 呼叫C語言 Demo

yunchat發表於2006-03-04

專案中由於mail送信部分要求使用C實現,並且使用訊息佇列通訊。 因此這就需要WEB_APP中呼叫C程式。 JAVA中如何呼叫C,這就使用到了JAVA提供的JNI介面。雖然從沒接觸過C,甚至學校裡也沒學過,但還是應客戶要求,我寫了個Demo。包含

JAVA透過JNI呼叫C,以及C透過JNII呼叫JAVA

關於訊息佇列的使用,這裡我就不作說明

JAVA呼叫C

無非如下步驟:

1.建立本地方法的JAVA程式碼。

2.建立原生程式碼對應的.h檔案。

3.實現可裝載的C程式碼。

[@more@]

〔事前條件〕

Web Server Application 執行送信、中止、通知的處理。

〔事後條件〕

傳送“訊息依賴”到訊息佇列中。等待Daemon接收。

〔處理內容〕

1C檔案裝載

編碼例

static { System.load(MailJni.o"); }

2.定義Native 方法

編碼例

public native boolean SendMailMsg(); public native boolean StopMailMsg(String deliverId); public native boolean sendMailNotice();

3MailJni類作成jp_co_docomo_sms_common_util_MailJni.h標頭檔案。

操作命令

Javah –jni jp.co.docomo.sms.common.util.MailJni

4. 作成可裝載的C程式碼。

方法名及引數要與原生程式碼對應的.h檔案的定義相對應。

JAVA CLASS

// Copyright(c) 2006.

//

// $Id:$

// $Log:$

//

package jp.co.docomo.sms.common.util;

/**

* MailJniEB

*

* @version $Revision:$ $Date:$

*/

public class MailJni {

public MailJni() {

}

public native boolean SendMailMsg();

public native boolean StopMailMsg(String deliverId);

public native boolean sendMailNotice();

static {

// System.loadLibrary("MailJni.o");

// Linux hack, if you can't get your library

// path set in your environment:

System.load(

"/usr/eclipse/workspace/MAILJNI/MailJni.o");

}

public static void main(String[] args) {

System.out.println("MailJni starting; args.length" + args.length);

MailJni mailJni= new MailJni();

boolean re= mailJni.sendMailNotice();

System.out.println("Back in java");

}

}

MailJni.c

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include "msg_ipc.h"

#include "./jp_co_docomo_sms_common_util_MailJni.h"

jboolean Java_jp_co_docomo_sms_common_util_MailJni_SendMailMsg(JNIEnv* env,

jobject this) {

int crtdmnid = msgcreate("/var", 'a', IPC_CREAT|00666);

msgsbuf *msg_sbuf;

msg_sbuf = (msgsbuf*)malloc(sizeof(msgsbuf));

char text[] ="send";

msg_sbuf->mtype = 2;

strcpy (msg_sbuf->mtext, text);

if (sendmessages(crtdmnid, msg_sbuf,0) == -1) {

return 0;

}

printf("send 'send mail' msg to create Daemon n");

return 1;

}

jboolean Java_jp_co_docomo_sms_common_util_MailJni_StopMailMsg(JNIEnv* env,

jobject this, jstring jMsg) {

const char* id=(*env)->GetStringUTFChars(env,jMsg,0);

int crtdmnid = msgcreate("/var", 'a', IPC_CREAT|00666);

msgsbuf *msg_sbuf;

msg_sbuf = (msgsbuf*)malloc(sizeof(msgsbuf));

char text[10];

strcpy(text, id);

msg_sbuf->mtype = 1;

strcpy (msg_sbuf->mtext, text);

if (sendmessages(crtdmnid, msg_sbuf,0) == -1) {

return 0;

}

printf("send 'stop mail' msg to create Daemon n");

return 1;

}

jboolean Java_jp_co_docomo_sms_common_util_MailJni_sendMailNotice(JNIEnv* env,

jobject this) {

printf("call method from java n");

int notdmnid = msgcreate("/var", 'e', IPC_CREAT|00666);

msgsbuf *msg_sbuf;

msg_sbuf = (msgsbuf*)malloc(sizeof(msgsbuf));

char text[] ="notice";

msg_sbuf->mtype = 1;

strcpy (msg_sbuf->mtext, text);

if (sendmessages(notdmnid, msg_sbuf,0) == -1) {

return 0;

}

printf("send 'notice mail' msg to Notice Daemon: %dn", notdmnid);

return 1;

}

msg_ipc.h

#ifndef MSG_IPC_H_

#define MSG_IPC_H_

#include

typedef struct msgsbuf

{

long mtype;

char mtext[10];

}msgsbuf;

typedef struct msgrbuf

{

long mtype;

char mtext[10];

}msgrbuf;

int sendmessages(int msqid, struct msgsbuf *sbuf, int msgflg)

{

int length;

length = sizeof(struct msgsbuf) - sizeof(long);

return msgsnd (msqid, sbuf, length, msgflg);

}

int readmessage(int msqid, int type, struct msgrbuf *rbuf, int msgflg)

{

int length;

length = sizeof(struct msgrbuf) - sizeof(long);

return msgrcv(msqid, rbuf, length, type, msgflg);

}

int msgcreate(char* msgpath, char proj, int flags) {

key_t key=ftok(msgpath,proj);

int msgid=msgget(key,flags);

if(msgid==-1)

{

printf("msg create errorn");

}

return msgid;

}

#endif /*MSG_IPC_H_*/

Make file

# Author yunchat yunchat@hotmail.com

# (c) Bruce Eckel 2000

# Copyright notice in Copyright.txt

# Automatically-generated MAKEFILE

# For examples in directory .appendixb

# using the JDK 1.2 compiler

# Invoke with: make

JVC = javac

JVCFLAGS =

.SUFFIXES : .class .java

.java.class :

$(JVC) $(JVCFLAGS) $<

# Microsoft Visual C++:

CPP=cl

DLLFLAG=-LD

OFLAG=-Fe

# Borland C++. A command-line version of this compiler

# is available for free download from

#

#CPP=bcc32

#DLLFLAG=-WD

#OFLAG=-o

# IMPORTANT: Adjust this for your environment:

#WININCLUDE = -Ic:/ProgTools/Java/include -Ic:/ProgTools/Java/include/win32

UNIXINCLUDE = -I/usr/local/j2sdk1.4.2_08/include -I/usr/local/j2sdk1.4.2_08/include/linux

#dos:

# ShowMessage.class

# UseObjects.class

# MailJni.class

# MsgImpl.dll

# UseObjImpl.dll

# MailJni.dll

linux:

./jp/co/docomo/sms/common/util/MailJni.class

MailJni.o

MailJni.class: ./jp/co/docomo/sms/common/util/MailJni.java

MailJni.h: MailJni.class

javah -jni jp.co.docomo.sms.common.util.MailJni

#MsgImpl.dll: MsgImpl.cpp ShowMessage.h

# $(CPP) $(WININCLUDE) $(DLLFLAG) MsgImpl.cpp $(OFLAG)MsgImpl.dll

#UseObjImpl.dll: UseObjImpl.cpp UseObjects.h

# $(CPP) $(WININCLUDE) $(DLLFLAG) UseObjImpl.cpp $(OFLAG)UseObjImpl.dll

MailJni.o: MailJni.c MailJni.h

gcc -o MailJni.o -shared -Wl,-soname,MailJni.o $(UNIXINCLUDE)

MailJni.c -static -lc

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/118026/viewspace-818300/,如需轉載,請註明出處,否則將追究法律責任。

相關文章