C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++调用Java的Jar包

浅析C++调用Java的Jar包(带参数)问题

作者:alwaysmove

这篇文章主要介绍了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包名称<br>          @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包(带参数)的文章就介绍到这了,更多相关C++调用Java的Jar包内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文