Java代码获取在Mac OS x上安装Firefox的路径

时间:2013-10-03 09:16:56

标签: java macos terminal

我需要在Mac上找到安装Firefox应用程序的目录,我运行终端命令:

find / -name Firefox.app 2>/dev/null

现在我需要在java程序中运行相同的命令,我的代码是:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class kill{
        public static void main(String[] args) throws Exception{
                String cmds[] = {"find","/","-name","Firefox.app"};

        Process p = Runtime.getRuntime().exec(cmds);
                p.waitFor();
                //int exitVal = p.waitFor();
                //System.out.println("Process exitValue:" + exitVal);
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(
                p.getInputStream()));
                String line = reader.readLine();
                while (line != null) {
                line = reader.readLine();
                System.out.println(line);
                }
}
}

但它并没有让我回归。任何人都可以告诉我这里有什么问题..任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

循环中存在逻辑错误。应该是这样的:

String line = reader.readLine();
while (line != null) {
    System.out.println(line);
    line = reader.readLine();
}

尽管如此,Firefox总是应该在/ Applications中。