如何使用Java输入用户输入运行c执行文件Runtime().exec()
。如果没有我的C程序执行文件中的用户 输入运行在Runtime().exec()
:在Runtime.exec()中运行执行文件并获取用户输入
例如:下面的C程序如果n是预定义的,它的执行文件中 Java运行时()运行...如果n是用户输入如何运行执行文件?
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main()
{
setbuf(stdout, NULL); //set buffer null
int i,n;
scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("%d\n",i);
sleep(1); //delay
}
} //main
Java代码....
import java.io.*;
import java.lang.Runtime;
import java.lang.*;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
public class run3
{
public static void main(String[] args)
{
try
{
String[] cmd = {"file path"};
Process p = java.lang.Runtime.getRuntime().exec(cmd);
String s;
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null)
{ System.out.println(s); }
System.out.println("Done.");
}//try
catch (IOException ex)
{ ex.printStackTrace(); }
} //void
} //main
为了上帝的缘故,使用'ProcessBuilder'! –
Java Native Interface ??? http://techforum4u.com/content.php/390-Steps-to-Call-a-C-Program-from-Java – SenthilPrabhu
@Senthil Prabhu。没有JNI –