No output when running native OS command with Java

I need to run a command from my java application and process its output. The code is as follows:

public static void readAllOutput(){
        try {
            final String cmd = new String("find ~ -iname \"screen*\"");
            System.out.println(cmd);
            Process ps = Runtime.getRuntime().exec(cmd);
//          ps.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException /*| InterruptedException*/ e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

When I execute this command from the OS, I have a lot of output, but in my Java application the output is emty.

+4
source share
1 answer

you need to use

getRuntime().exec( new String[] { "find", "~", "-iname","screen*"} );

or try

getRuntime().exec( new String[] { "find", "~", "-iname","\"screen*\""} );

inorder for accepting arguments in double quotes.

+2
source

Source: https://habr.com/ru/post/1614112/


All Articles