I want to pass the parameters from my java program to C program which is execute as command line argument. This is my C program,
#include <stdio.h>
int main( int argc, char *argv[] ) {
if( argc == 2 ) {
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
}
else {
printf("One argument expected.\n");
}
}
This is output I got when I compiled it from terminal.
lclab@lclab:~/Desktop/jni$ ./a.out param1
The argument supplied is param1
I want to pass parameters(param1) to this program from my java program. How can I do that? I tried with java process builder but it always return -1.
try {
ProcessBuilder processBuilder =
new ProcessBuilder("gcc", "/home/lclab/Desktop/jni/test.c", "param1");
Process proc = processBuilder.start();
System.out.println(proc.getInputStream().read());
return proc.getInputStream().read();
} catch (IOException e) {
e.printStackTrace();
return 99;
}
My machine is ubuntu.