gpt4 book ai didi

linux - Java执行linux命令

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:49:05 25 4
gpt4 key购买 nike

<分区>

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

你好,我使用 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)

你能告诉我哪里出了问题吗?非常感谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com