浅析C++调用Java的Jar包(带参数)问题
短信预约 -IT技能 免费直播动态提醒
JarAppCall.h
#include <atlstr.h>
class CJarAppCall
{
public:
CJarAppCall(void);
~CJarAppCall(void);
bool Run(const CString& strJarDir, const CString& strJarName, const CString& strParam);
private:
bool CallApp(const CString& strJarName, const CString& strParam);
};
JarAppCall.cpp
#include "JarAppCall.h"
CJarAppCall::CJarAppCall(void){
}
CJarAppCall::~CJarAppCall(void){
}
bool CJarAppCall::Run(const CString& strJarDir, const CString& strJarName, const CString& strParam){
//缓存当前文件夹
WCHAR sOldDir[MAX_PATH] = { 0 };
GetCurrentDirectory(MAX_PATH, sOldDir);
//设置当前文件夹为jar包文件夹
SetCurrentDirectory(strJarDir);
bool bCalRest = CallApp(strJarName, strParam);
//还原当前文件夹
SetCurrentDirectory(sOldDir);
return bCalRest;
}
bool CJarAppCall::CallApp(const CString& strJarName, const CString& strParam){
WCHAR sSysDir[MAX_PATH] = { 0 };
GetSystemDirectory(sSysDir, MAX_PATH);
CString strFullPath = sSysDir;
strFullPath += _T("\\cmd.exe");
CString strCmdLine = _T(" /C ");
strCmdLine += _T("java -jar ");
strCmdLine += strJarName;
strCmdLine += _T(" ");
strCmdLine += strParam;
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
BOOL bRunProgram = CreateProcess(strFullPath.GetBuffer(),
strCmdLine.GetBuffer(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
strFullPath.ReleaseBuffer();
strCmdLine.ReleaseBuffer();
if (!bRunProgram){
return false;
}
// 等待程序结束
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
return true;
}
test.cpp
#include "stdio.h"
#include "JarAppCall.h"
#include<stdlib.h>
#include <cstring>
#include <string>
#include <iostream>
int charContainsToChar(char* source, char* target);
char* getNameReferences(char* class="lazy" data-src);
int main(){
//char class="lazy" data-src[] = "测.试.xlsx";
//char test[] = "test";
//char dataset_name[] = "PLM_021-000320.txt";
//char* result = getNameReferences(test);
//char* null = "NULL";
//if (strcmp(result, null) != 0){
// printf("name=%s\n", 1);
//}
//printf(result);
CString strJarDir = _T("C:\\Users\\12449\\Desktop");
CString strJarName = _T("HFAcceptAndSendHandlerParam.jar");
CString strParam = _T("test");
CJarAppCall jarCall;
if (!jarCall.Run(strJarDir, strJarName, strParam)){
printf("文件发送失败!");
}
else{
printf("文件发送成功!");
}
return 0;
}
char* getNameReferences(char* class="lazy" data-src){
char* result = "\0";
if (strcmp(class="lazy" data-src, "\0") == 0){
return "NULL";
}
char point[] = ".";
if (charContainsToChar(class="lazy" data-src, point) > 0){
printf("判断成功!");
}
char *p = strstr(class="lazy" data-src, ".");
p++;
while (charContainsToChar(p, point) > 0){
printf("判断成功!");
p = strstr(p, ".");
printf("执行!");
p++;
}
char *xls = "xls";
char *xlsx = "xlsx";
char *doc = "doc";
char *docx = "docx";
char *ppt = "ppt";
char *pptx = "pptx";
char *pdf = "pdf";
char *zip = "zip";
char *rar = "rar";
char *txt = "txt";
if (strcmp(p, xls) == 0){
result = "excel";
}
else if (strcmp(p, xlsx) == 0){
result = "excel";
}
else if (strcmp(p, doc) == 0) {
result = "word";
}
else if (strcmp(p, docx) == 0){
result = "word";
}
else if (strcmp(p, ppt) == 0){
result = "powerpoint";
}
else if (strcmp(p, pptx) == 0){
result = "powerpoint";
}
else if (strcmp(p, pdf) == 0){
result = "PDF_Reference";
}
else if (strcmp(p, zip) == 0){
result = "ZIPFILE";
}
else if (strcmp(p, rar) == 0){
result = "HF8_RAR";
}
else if (strcmp(p, txt) == 0){
result = "Text";
}
return result;
}
//判断字符串中是否包含另一个字符串
int charContainsToChar(char* source, char* target)//source是主串,target是子串
{
int i, j;
int s_len = strlen(source);
int t_len = strlen(target);
if (t_len>s_len)
{
return -1;
}
for (i = 0; i <= s_len - t_len; i++)
{
j = 0;
int flag = 1;
if (source[i] == target[j])
{
int k, p = i;
for (k = 0; k<t_len; k++)
{
if (source[p] == target[j])
{
p++;
j++;
continue;
}
else
{
flag = 0;
break;
}
}
}
else
{
continue;
}
if (flag == 1)
{
return i;
}
}
return -1;
}
到此这篇关于C++调用Java的Jar包(带参数)的文章就介绍到这了,更多相关C++调用Java的Jar包内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341