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 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response, but I have already tried this query as mentioned in the the question. It throws the same error.
Well, I only paid attention to your code and ignored what you described. I have modified the answer, I think this can help you.
May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.