简体   繁体   English

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

[英]Java Code to Get path to installed Firefox on Mac OS x

I need to find the directory where Firefox Application is installed on Mac, for which i run the terminal command : 我需要找到在Mac上安装Firefox应用程序的目录,并在该目录中运行terminal命令:

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

Now i need to run the same command in a java program, My code is : 现在我需要在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);
                }
}
}

But it does not return me the path. 但这并没有给我返回道路。 Can anyone tell me what is wrong in here.. Any help would be appreciated 任何人都可以告诉我这里出了什么问题..任何帮助将不胜感激

There is a logic error in your loop. 您的循环中存在逻辑错误。 It should be like: 应该是这样的:

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

Although, Firefox should always be in /Applications anyways. 虽然,无论如何,Firefox应该始终位于/ Applications中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM