Browser Version Detection using Command line method using java

Viewed 332

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 Answers

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

Related