Search code examples
javalinuxshellubuntudarknet

communicating with the process while executing a shell command with java


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());
    } 
}

Solution

  • You need to call writer.flush() in order to actually output something to the underlining InputStream

    Therefore your code should look like:

    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();
            // **** add flush here ****
            writer.flush();
            // and remember to close your resource too
            writer.close();
            //reader.readLine();
            while ((line = reader.readLine()) != null) {
                System.out.print(line + "\n");
                s.add(line);
            }
            // ***** close your reader also ****
            reader.close();
            //            proc.waitFor();
        } catch (IOException e) {
            System.out.println("exception thrown: " + e.getMessage());
        } 
    }