如何通过每次分派新的subprocess多次运行同一个jar文件?

如何通过每次分派新的subprocess多次运行同一个jar文件?,第1张

概述如何通过每次分派新的subprocess多次运行同一个jar文件

我正在尝试编写一个C ++程序。 该程序需要运行一些jar文件,每运行一次jar文件两次。 问题是程序正确运行每个文件一次。 在第二次我得到一个错误消息,就像虚拟机得到了一个错误的命令。 但是,这是第一次运行jar文件的命令!

这是我得到的红色错误消息的前几行。

Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file) where options include: -d32 use a 32-bit data model if available -d64 use a 64-bit data model if available -clIEnt to select the "clIEnt" VM -server to select the "server" VM

这是我的代码。 这是主要文件:

#include <iostream> #include <fstream> #include <signal.h> #include <string.h> #include <time.h> #include "StringParser.h" #include "ShellCore.h" using namespace std; int main() { while (1) { char checkers_command[] = "java -jar /home/samer/Downloads/chkr.jar"; char castle_command[] = "java -jar /home/samer/Downloads/cas.jar"; int child_status; bool wait_bool = true; cout << "Run Batch Shell> "; cin.getline(input_command_line,256); if (strcasecmp("exit",input_command_line) == 0) { cout << "The shell program will terminaten"; exit(0); } else { char* commands; for (int var = 0; var < 4; ++var) { if (var % 2 == 0) { commands = checkers_command; } else if (var % 2 == 1) { commands = castle_command; } char* arg_List_tokened[50]; parse_command_line(arg_List_tokened,commands); spawn(arg_List_tokened[0],arg_List_tokened); if (wait_bool) { // The parent wait until the child finishes. cout << "The parent will wait for " << arg_List_tokened[0] << endl; wait(&child_status); if (WIFEXITED(child_status)) { printf("The child process exited normally with exit code %dn",WEXITSTATUS(child_status)); } else { printf("The child process exited abnormallyn"); } } } cout << "donen"; } } return 0; }

这是Shellcore.cpp文件中的“spawn”方法:Shellcore.h包含Shellcore.cpp所需的包含。

什么是最好的方式来在Win上分叉/线程的PHP?

用Java分叉和放弃特权

如何保持同一个核心上的父母和子女的过程

Mac上的叉(OSX-10.9.2)与默认的编译器(gcc-4.2)有什么不同吗?

fork()和STDOUT / STDERR从subprocess到控制台

#include "ShellCore.h" pID_t child_pID; int spawn(char* program_name,char** args) { child_pID = fork(); if (child_pID != 0) { return child_pID; } else { execvp(program_name,args); // If the execvp return,this indicate that an error occurred. printf("From the spawn function in the parent process,execvp ERRORn"); abort(); } }

很抱歉,长问题和长码。 感谢提前:)

parsing函数:

voID parse_command_line(char* output_List[],char* command_line) { char * pch; int i = 0; pch = strtok(command_line," &""); output_List[0] = pch; while (pch != NulL) { i++; pch = strtok(NulL," &""); output_List[i] = pch; } output_List[++i] = NulL; }

删除shell脚本中的分支,以便在Cygwin中运行良好

防止fork()复制套接字

为什么fork()和printf()的输出比我预测的要多?

双叉使用vfork

linux的fork函数与windows的CreateProcess相比 – 什么被复制?

当你调用它时,strtok()会改变你的字符串。 您将指针“命令”设置为checkers_command或castle_command,但strtok只是要覆盖指向每个后者的内存。 如果你复制棋子/城堡而不是在调用你的解析函数之前指向它,那么每次在循环中都会有一个新的棋子/城堡副本。

比strdup有更好的方法来做这个,但是为了简洁起见,你可以简单地做到这一点。 如果保留这种复制字符串(不可取)的方法,请记住在完成对返回的指针时调用free(),否则将发生内存泄漏。

改成这样的东西:

for (int var = 0; var < 4; ++var) { if (var % 2 == 0) { //commands = checkers_command; commands = strdup(checkers_command); } else if (var % 2 == 1) { //commands = castle_command; commands = strdup(castle_command); } char* arg_List_tokened[50]; parse_command_line(arg_List_tokened,arg_List_tokened); //............ }

总结

以上是内存溢出为你收集整理的如何通过每次分派新的subprocess多次运行同一个jar文件?全部内容,希望文章能够帮你解决如何通过每次分派新的subprocess多次运行同一个jar文件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1154798.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存