您好,我使用 exec 命令编写了简单的 java 函数。此功能检查系统(linux)中是否存在字体。首先我写了简单的 bash 命令:identify -list font | grep -i 'Font: Times-Bold' -w
它的工作很完美,所以我创建了简单的程序:
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();
}
}
它的工作,但我不认为。此函数返回我系统中存在的所有字体。似乎命令 grep 不是 exec ?
我尝试使用我创建的另一个版本的命令 exec():
String command = {"identify -list font", "grep -i -w \'Font: " + fontName + "\'"}
但我有错误:
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)
你能给我建议什么是错的吗?多谢