2

Possible Duplicate:
How to make pipes work with Runtime.exec()?

Hello i wrote simple java function using exec command. This function check that font exist in system (linux). First i wrote simple bash command : identify -list font | grep -i 'Font: Times-Bold' -w and its work perfectly so i create simple program:

public abstract class SystemReader{

    public static final void checkFontExist(String name){
            String command = "identify -list font | grep -i -w \'Font: "  + name + "\'";
            Process p  =Runtime.getRuntime().exec(command);

            String lines = "";
            String resoults ="";
            BufferedReader bufferedReader = new BufferedReader(new      InputStreamReader(p.getInputStream()));
            while((line  buferedReader.readLine())!=null){
                    resoult += line + "\n";
            }

            System.out.println("RESPONSE: " + resoult);
            bufferreader.close();
    }

}

Its working but not i espect. This function return all fonts exist in my system . Its seems that command grep is not exec ?

i try use another version of command exec() i create :

String command = {"identify -list font", "grep -i -w \'Font: " + fontName + "\'"}

but i have got error :

Exception in thread "main" java.io.IOException: Cannot run program "identify -list font ": java.io.IOException: error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:466)

Can you give me advice what is wrong ? thanks a lot

1
  • piping is provided by the shell so you'll need to pass the results from one command into the other manually. Commented Aug 23, 2012 at 17:33

1 Answer 1

8
String[] cmd = {
    "/bin/sh",
    "-c",
    "identify -list font | grep -i -w \'Font: "  + name + "\'"
};

Process p = Runtime.getRuntime().exec(cmd);

Will pass the command through the shell. Which is what you need since the | (pipe) command is provided by the shell and not the Operating System.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.