allelopath
asked on
Display html file using SystemBrowser class
I'm trying to display an html file using the SystemBrowser class below.
I get the following error:
Could not invoke browser, command=netscape -remote openURL(file:/Users/joeUse r/Document s/myFile.h tm)
Caught: java.io.IOException: Cannot run program "netscape": error=2, No such file or directory
The file is definitely there.
This is in OS X.
Any suggestions?
I get the following error:
Could not invoke browser, command=netscape -remote openURL(file:/Users/joeUse
Caught: java.io.IOException: Cannot run program "netscape": error=2, No such file or directory
The file is definitely there.
This is in OS X.
Any suggestions?
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import Constants;
/**
* Class to display html file in the system browser
*/
public class SystemBrowser implements Constants {
// Used to identify the windows platform.
private static final String WIN_ID = "Windows";
// The default system browser under windows.
private static final String WIN_PATH = "rundll32";
// The flag to display a url.
private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
// The default browser under unix.
private static final String UNIX_PATH = "netscape";
// The flag to display a url.
private static final String UNIX_FLAG = "-remote openURL";
/**
* Display a file in the system browser.
* If you want to display a file, you must include the absolute path name.
*
* @param url the file's url (the url must start with either "http://" or "file://").
*/
public static void displayURL(URL url) {
logger.info("SystemBrowser.displayURL() = " + url);
final boolean isWindows = isWindowsPlatform();
String commandString = null;
try {
if (isWindows) {
// commandString = 'rundll32 url.dll,FileProtocolHandler http://...'
commandString = WIN_PATH + " " + WIN_FLAG + " " + url;
logger.info(commandString);
final Process p = Runtime.getRuntime().exec(commandString);
}
else { // not windows
// Under Unix, Netscape has to be running for the "-remote" command to work.
// So try sending the command and check for an exit value.
// If the exit command is 0, it was successful otherwise we need to start the browser.
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
commandString = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
Process process = Runtime.getRuntime().exec(commandString);
try {
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
final int exitCode = process.waitFor();
if (exitCode != 0) {
// Command failed, start up the browser
// cmd = 'netscape http://www.javaworld.com'
commandString = UNIX_PATH + " " + url;
process = Runtime.getRuntime().exec(commandString);
}
}
catch(final InterruptedException x) {
System.err.println("Error bringing up browser, cmd='" + commandString + "'");
System.err.println("Caught: " + x);
logger.info("Error bringing up browser, cmd='" + commandString + "'");
logger.info("Caught: " + x);
}
}
}
catch(final IOException x) {
logger.info("Could not invoke browser, command=" + commandString);
logger.info("Caught: " + x);
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + commandString);
System.err.println("Caught: " + x);
}
}
/**
* see if running Windows
*
* @return true if this application is running under a Windows OS
*/
public static boolean isWindowsPlatform() {
final String osName = System.getProperty("os.name");
if ( osName != null && osName.startsWith(WIN_ID)) {
return true;
}
else {
return false;
}
}
/**
* main
*/
public static void main(String[] args) {
try {
displayURL(new URL("http://epswww.unm.edu/facstaff/dolomite/"));
} catch (final MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ASKER
>>"file is definitely there".
I mean the url passed in as a parameter
I have firefox installed. I tried substituting "firefox" for "netscape", but same error.
I mean the url passed in as a parameter
I have firefox installed. I tried substituting "firefox" for "netscape", but same error.
No it is probably not that simple, I guess, as minimum you need to provide the path to firefox.
I don't know OS X well enough. Let's wait for someone who knows it.
I had the impression that OS X is based on Linux, do you start your java application from command line?
I don't know OS X well enough. Let's wait for someone who knows it.
I had the impression that OS X is based on Linux, do you start your java application from command line?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@aciuica
Yes, that is probably a good idea.
I even looked at that same web page, but did not realize, that they grab the system browser from the system itself.
Yes, that is probably a good idea.
I even looked at that same web page, but did not realize, that they grab the system browser from the system itself.
@for_yan
Yes, and it seems that there is a Java 5 solution too (with some Mac Os special handling) :)
Yes, and it seems that there is a Java 5 solution too (with some Mac Os special handling) :)
ASKER
I can count on Java 6, but I have encountered problems with the Desktop class in the very situation on Windows XP, which this also needs to work for.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Do you mean that your URL file is there?
It looks like it cannot reach netscape, the browser itself - do you have netscape installed and configured?