Monday, April 22, 2013

java quickie: launching webpage inside chrome, specifically

I needed to open a webpage from a Java app. Some code I inherited did this:

Not bad but it launches the default web browser (usually Safari on OSX). Luckily this app was OSX only, so I could rely on the open command. I ended up with:

String cmds[] = {"open","-a","Google Chrome",WEBAPP_URL};
Process p = Runtime.getRuntime().exec(cmds);
if(p.exitValue() != 0){
     //show message, then try old method:                         

The tricky bit was breaking up the command into an array; without that, the space in "Google Chrome" was messing things up and I couldn't figure out how to make a single big command String that respect that space.

One the command runs we wait for it to finish and then go to the old behavior if it seemed to fail, according to the process exit value.

No comments:

Post a Comment