需求:在Linux C/C++中调用jar包,并传递参数,将执行结果返回。

实现思路:在脚本中执行java jar命令 同时在代码中调用脚本并传递参数

1. 脚本send_message.sh

#!/bin/sh
#send_message.sh

JAR_PATH=/usr/tmp/sendMsg.jar

#    $1  ip
#    $2  port
#    $3  account
#    $4  password
#    $5  phone
#    $6  context

#参数个数不能小于6个
if [ $# -lt 6 ]; then
     exit 1
fi

java -jar $JAR_PATH $1 $2 $3 $4 $5 $6

exit $?

2. 代码片段

static const char* SEND_MESSAGE ="/usr/tmp/send_message.sh";

int SendSms(CString cmd)
{
	int ret = 0;
	ret = system(cmd);
	if(ret != 0 ){
		return -1;
	}
	
	return 0;
}

int SmsCmdCmccA()
{
	CString strSendSms = "";
        CString ip = "******";
        int port = ***;
        CString account = "******";
        CString password = "******";
        CString phone = "******";
        CString content = "******";
	strSendSms.nFormat(1024, "sh %s \'%s\' %d \'%s\' \'%s\' \'%s\' \'%s\'", 
					   (LPCTSTR) SEND_MESSAGE, 
					   (LPCTSTR) ip,
					    port,
					   (LPCTSTR) account,
					   (LPCTSTR) password,
					   (LPCTSTR) phone,
					   (LPCTSTR) content);
	 
	return SendSms(strSendSms);
}

 脚本存放位置:/usr/tmp/send_message.sh

 jar包存放位置:/usr/tmp/sendMsg.jar

Logo

更多推荐