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

Community
  • 1
  • 1
Łukasz Woźniczka
  • 1,625
  • 3
  • 28
  • 51

1 Answers1

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.

Randall Hunt
  • 12,132
  • 6
  • 32
  • 42