Java教程

C++调用Java的Jar包(带参数)

本文主要是介绍C++调用Java的Jar包(带参数),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

JarAppCall.h

#include <atlstr.h>
class CJarAppCall
{
	public:
		CJarAppCall(void);
		~CJarAppCall(void);
		/*
		@brief 运行Jar包调用,等待运行完毕返回
		@param[in] strJarDir jar包文件夹
		@param[in] strJarName jar包名称
          @param[in] strParam 传入的参数
		@return 运行是否成功
		*/
		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* src);

int main(){
	/*CString strJarDir = _T("C:\\Users\\12449\\Desktop");
	CString strJarName = _T("test.jar");

	CJarAppCall jarCall;
	if (!jarCall.Run(strJarDir, strJarName)){
		//启动失败
		printf("启动失败");
		return 0;
	}
	printf("启动成功");*/
	

	//char 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* src){
	char* result = "\0";
	if (strcmp(src, "\0") == 0){
		return "NULL";
	}
	char point[] = ".";
	if (charContainsToChar(src, point) > 0){
		printf("判断成功!");
	}
	char *p = strstr(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包(带参数)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!