ITPub博客

首页 > 应用开发 > IT综合 > JAVA JNI 调用C语言 Demo

JAVA JNI 调用C语言 Demo

原创 IT综合 作者:yunchat 时间:2006-03-04 14:46:30 0 删除 编辑

项目中由于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

# http://www.borland.com/bcppbuilder/freecompiler/

#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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    32
  • 访问量
    955739