public class Controller {
public String printResults(Process process) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
String container = "";
while ((line = reader.readLine()) != null) {
container = container + line + "\n";
System.out.println(line);
}
return container;
}
public String executeCmd(String firstname, String lastname) throws IOException {
String command = "./myprogram -n " + firstname + " -s " + lastname;
Process p = Runtime.getRuntime().exec(command, null, new File("/home/user/myprogram/build/"));
return "Hi, \n" + printResults(p);
}
}
This allowes me to run a single command with attributes from a specific directory and get back result.
However, I have a larger program, which asks user for inputs from terminal.
user@debian:~/program/build/$./program
...
Enter Value: 5
...
Enter Name: Hanz
...
Enter State: GE
Output..
How can I run that program and enter user input from Java?
execve-- uses an array of strings; your code should do the same. Otherwise, you need to worry about whether you have, say, afirstnameof$(rm -rf ~)