I'm going to execute a shell command from java and i need to pass arguments to the output stream while executing the command..
following is the shell command
./darknet detect cfg/yolo-voc.2.0.cfg backup/yolo-voc_20000.weights
when executing this command it is yielding for the path of the image file in the terminal i can provide the path of the image as follows
Loading weights from backup/yolo-voc_21000.weights...Done!
Enter Image Path:
when executing from the terminal i can provide the path there.
I managed to execute this command withing the java process and also i can get an output when i provide an image uri with the command. here is the code
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}
but what I want is provide the image path at the runtime not beginning of the execution of the command.. tried with writing to the output stream as below still no luck
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
writer.append("data/test2.jpg");
writer.newLine();
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}
You need to call
writer.flush()
in order to actually output something to the underliningInputStream
Therefore your code should look like: