服务器之家

服务器之家 > 正文

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

时间:2022-02-23 14:31     来源/作者:alwaysmove

JarAppCall.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#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包内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/wwssgg/p/15565439.html

标签:

相关文章

热门资讯

蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部