Open Url in given browser using process builder

103 Views Asked by At

My use case is to use process builder in java springboot to open a test url in google chrome. I have explored various commands to do the same but not able to achieve a desired result. I have explored the following ways to achieve the same. Can anyone suggest some more way of doing this both in Mac & Windows?

I have tried these 2 things, but will need a more robust way of doing it.

Error that I am receiving while using it :

/home/dell/Dokument/GitHub/Vaadin-DL4J-YOLO-Camera-Mail-Reporter/Vaadin-DL4J-YOLO-Camera-Mail-Reporter/Darknet/darknet
detect
cfg/yolov2-tiny.cfg
weights/yolov2-tiny.weights
data/cameraSnap.png
-thresh 0.3
ERROR!
Exited with error code : 0

I am using these 2 methods in IntelliJ:

ProcessBuilder ProcessBuilder pb = new ProcessBuilder("./xyz", "devices", "-l")

OR

ProcessBuilder ProcessBuilder pb =
   new ProcessBuilder("myCommand", "myArg1", "myArg2");

Can anyone suggest a way to do this in both mac & windows ?

3

There are 3 best solutions below

1
chetan On BEST ANSWER

I am not sure if this is what u r asking but I can share some commands which u can try to open a URL in a given browser using process builder in mac. (for windows) ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c", "start", browser, url);

(for mac) ProcessBuilder processBuilder = new ProcessBuilder("open", "-a", browser, url);

0
pallav dubey On

Yes the commands works , also some other options have various functionality -e : open text edit -f : read input from standard input. Also whicle testing in google chrome use Google%20Chrome but in other cases we can use Safari & Firefox as usual browser names.

0
Torge Rosendahl On

Do you really want/need to force the user to a specific browser?

For cross-platform stuff it is usually a bad idea to use command-line instructions via process builder, because the commands will always differ a bit for different operation systems.

Could you use the Desktop API? Desktop.browse() will open any URL you give it in the users default browser.


See this question: https://stackoverflow.com/a/602045/10875738

Snippet:

import java.awt.Desktop;

//...

void showGoogle() {
    try {
        Desktop.getDesktop().browse(new URL("http://www.google.com").toURI());
    } catch (Exception e) {
        //TODO
    }
}