Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to run same jar file multiple times by forking new child process every time?
    primarykey
    data
    text
    <p>i am trying to write a c++ program. the program is required to run some jar files, run every jar file two times. the problem is that the program runs every file just one time correctly. at the second time i got an error message, as if the VM got a wrong command. However, it is the same command that run the jar files the first time !!!</p> <p>This is the first few lines of the red-colored error message that i got.</p> <pre><code>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 </code></pre> <p>This is my code. This is the main file:</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;signal.h&gt; #include &lt;string.h&gt; #include &lt;time.h&gt; #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 &lt;&lt; "Run Batch Shell&gt; "; cin.getline(input_command_line, 256); if (strcasecmp("exit", input_command_line) == 0) { cout &lt;&lt; "The shell program will terminate\n"; exit(0); } else { char* commands; for (int var = 0; var &lt; 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 &lt;&lt; "The parent will wait for " &lt;&lt; arg_list_tokened[0] &lt;&lt; endl; wait(&amp;child_status); if (WIFEXITED(child_status)) { printf("The child process exited normally with exit code %d\n", WEXITSTATUS(child_status)); } else { printf("The child process exited abnormally\n"); } } } cout &lt;&lt; "done\n"; } } return 0; } </code></pre> <p>This is the "spawn" method in the Shellcore.cpp file: The Shellcore.h contains the required includes for the Shellcore.cpp.</p> <pre><code>#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 ERROR\n"); abort(); } } </code></pre> <p>sorry for the long question and long code. Thanks is advance :)</p> <p>The parse function:</p> <pre><code>void parse_command_line(char* output_list[], char* command_line) { char * pch; int i = 0; pch = strtok(command_line, " &amp;\""); output_list[0] = pch; while (pch != NULL) { i++; pch = strtok(NULL, " &amp;\""); output_list[i] = pch; } output_list[++i] = NULL; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload