1

I know the commands to get the browser version of Edge, Firefox and Chrome in mac using command line which is working properly on my system. Edge=/Applications/"Microsoft Edge.app"/Contents/MacOS/"Microsoft Edge" -version FireFox=/Applications/Firefox.app/Contents/MacOS/firefox -v Chrome=/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version

Chrome1=/Applications/"Google Chrome.app"/Contents/MacOS/"Google Chrome" --version

When I try with Firefox it successfully returns me the Firefox version. but when I try for the Edge or the chrome it throws error in the java program.

Cannot run program "/Application/"Google" error=2 no such file or directory

I tried the escaping the query as /Applications/\"Microsoft Edge.app\"/Contents/MacOS/\"Microsoft Edge\" -version.I am printing the same before passing as a query and I am exactly getting the same query which works if I directly copy paste it on the terminal.

below is the code snippet

Am I missing something?

    String line;
            Process process;
            String msEdgeVersion = null;
    
            /*process = Runtime.getRuntime().exec("/Applications/Firefox.app/Contents/MacOS/firefox -v");*/
    
            process = Runtime.getRuntime().exec(/Applications/"Microsoft Edge.app"/Contents/MacOS/"Microsoft Edge" -version);
    
            // process = Runtime.getRuntime().exec("where notepad");
    
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = br.readLine()) != null) {
                if (!StringUtils.isBlank(line)) {
                    msEdgeVersion = line;
                    System.out.println(line);
                }
            }
            process.waitFor();
            process.destroy();

1 Answers1

0

According to the related api Runtime.getRuntime().exec(String command), you need to know that the parameter passed in is a valid String, so you need to pay attention to the format of the String parameter type.

Edit:

I reproduced your problem and got the same problem. After searching, I found the same problem. This is because the Process is different from the terminal process. You have to add -l to run the command as logged in user.

This is my test: enter image description here

Xudong Peng
  • 1,463
  • 1
  • 4
  • 9
  • Thanks for the response, but I have already tried this query as mentioned in the the question. It throws the same error. – Sadanand Kolhe Aug 18 '21 at 04:45
  • Well, I only paid attention to your code and ignored what you described. I have modified the answer, I think this can help you. – Xudong Peng Aug 19 '21 at 01:42
  • May I know if you have got any chance to check my answer? I am glad to help if you have any other questions. – Xudong Peng Aug 23 '21 at 01:17
  • 1
    Thank you for the response, I solved this problem using passing String array with few modifications in the command. String command =/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge -version ; int lastSpaceIndex = command.lastIndexOf(" "); String[] commandArray = new String[]{command.substring(0, lastSpaceIndex),command.substring(lastSpaceIndex + 1)}; I am passing commandArray to the exec() which worked for me. – Sadanand Kolhe Aug 24 '21 at 03:56
  • I’m glad to hear that your issue has been resolved. If possible, you can post it as an answer, which may also help other members of the community. – Xudong Peng Aug 25 '21 at 01:31