Java return code not correctly written with C ++ () system call

Java Code:

public class Test {
    public static void main ( String[] args ) {   
         System.exit ( 4 );
    }
}

C ++ Code:

#include <stdio.h>
#include <cstdlib>

int main() {
    int result = system ( "java Test" );
    printf ( "Result: %d", result ); 
}

Running these c ++ programs

"Result: 1024"

Should I run the java program directly and check the return state value with bash $? variable i get 4:

> java Test
> echo $?
4

So, it seems I do not understand or am using the System call incorrectly.

How to get this return value of "4" from a call to system (), not "1024", but where does "1024" come from?

Thanks!

+4
source share
2 answers

You can use the WEXITSTATUSresult of the call systemto get the exit status.

#include <stdio.h>
#include <cstdlib>

int main() {
    int result = system ( "java Test" );
    printf ( "Result: %d", WEXITSTATUS( result ) );
}
+2
source

. system() , , .

. system(), waitpid(), , system().

0

Source: https://habr.com/ru/post/1613278/


All Articles