This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes JDIC opening any file with the browser Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Reply Bookmark "JDIC opening any file with the browser" Watch "JDIC opening any file with the browser" New topic
Author

JDIC opening any file with the browser

Sam Latious
Greenhorn

Joined: Sep 19, 2008
Posts: 4
This is on Mac OS X 10.5.5 using JDIC with Java 1.5 and Firefox is my default browser.

I'm trying to open up an XML file on the local file system using Firefox instead of the default XML viewer(there's a stylesheet). I don't want to set Firefox as my default XML viewer because for another part of this program, I DO want to open XML up with an editor.

Here's some code I've tried along with the results:

Desktop.browse("http://www.cnn.com"); //works fine

Desktop.browse("/Users/iAmAFile.xml");//Malformed URL expection:no Protocol

Desktop.browse("file:///Users/file.xml);
//Desktop Exception: Failed to Launch the Default Browser
//Also, this is what is in the URL bar when I manually open the file with
//firefox

Thanks for your help.

-Sam
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Check out Runtime.exec and ProcessBuilder. Simply running "firefox mypathto/myfile.xml" should work, as long as firefox is on your path.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Sam Latious
Greenhorn

Joined: Sep 19, 2008
Posts: 4
Thanks for the response. I think that would work on my machine, but I need this to work on Windows machines, ones that may use either firefox or IE. My bad for not putting that into the original post. I did think of a way to do this last night though.

This assumes that the machine is set up so that the default browser and the default program for opening HTML files is the same thing.

I plan on generating a small html file at runtime which will redirect to whatever XML file I want to show.

Desktop.open("temp.html");
//asks the OS to open the file with whatever opens html files, hopefully
//the browser

Contents of temp.html:
<html>
<head>
<meta http-equiv="Refresh" content="0; URL=/path/to/file.xml">
</head>
</html>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35252
    
    7
The cross-platform Bare Bones Browser Launcher might also be helpful.


Android appsImageJ pluginsJava web charts
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: JDIC opening any file with the browser
 
Similar Threads
Open url in a default browser
Using default JAAS Mechanism in Websphere makes applications to access the context path of the other
how can i open save file dialog box in firefox?
Exception related to JDIC.
"Start: Applet is not initialized" error when accessing applet file through web browser