下面的c程序有两个参数a,b如果它与用户输入匹配,则打印值。我尝试在 Java 中运行 c 执行文件ProcessBuilder
,但它不读取执行文件。
Java 代码
import java.io.*;
import java.lang.Runtime;
import java.lang.*;
import java.io.File;
import java.lang.ProcessBuilder;
public class arg
{
public static void main(String[] args)
{
try
{
ProcessBuilder pb = new ProcessBuilder("path","-args[1]", "-args[2]");
pb.redirectErrorStream(true);
Process p = pb.start();
String s;
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
OutputStream stdOutput = p.getOutputStream();
stdOutput.close();
while ((s = stdInput.readLine()) != null)
{ System.out.println(s); }
System.out.println("Done.");
stdInput.close();
}//try
catch (IOException ex) { ex.printStackTrace(); }
} //void
} //main
c代码
#include <stdio.h>
#include <sys/timeb.h>
#include <string.h>
main(int argc, char **argv)
{
setbuf(stdout, NULL);
int i=1,j,n;
char a,b;
for (i=0; i<argc; i++)
{ printf("%s\n", argv[i]); }
if(!strcmp(argv[1],"a"))
{
if(!strcmp(argv[2], "b"))
{
for( j = 0; j<= 4; j++ )
{ printf("Iteration[%d] %d\n",j, j); }
}
return 0 ;
}
} //main