Results 1 to 12 of 12
Thread: Opening files in java
- 04-17-2010, 03:32 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
Opening files in java
Hi,
I am developing an applicaiton which I would need to run on windows, mac and on various other OSs. Would I need to do something to paths when for e.g. reading files eg. I have paths like //myFolder//myTxtFile.txt. Does this cater for everything?
Moreover I am opening files in this way: Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "VirtualPortal.html");
But I think this works only on Windows. Is that right?
Could you please suggest other methods which would work on all OSs?
- 04-17-2010, 04:29 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you try those things in different OSs?
- 04-17-2010, 04:32 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Just to guide you,
works on Windows only.Java Code:Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "VirtualPortal.html");
If you want to execute on the other OSs, like UNIX and so on try the following.
Java Code:String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; String browser = null; for (int count = 0; count < browsers.length && browser == null; count++) { if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) { browser = browsers[count]; } } if (browser == null) { throw new Exception("Could not find web browser"); } else { Runtime.getRuntime().exec(new String[]{browser, file_url}); }
- 04-17-2010, 08:45 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
hi,
thanks for the reply. I ve got a problem here if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0)
what should I write instead of "which" please? or is it supposed to work as it is? It is giving me the following error:
java.io.IOException: Cannot run program "which": CreateProcess error=2, The system cannot find the file specified
- 04-18-2010, 02:30 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you show me your complete code. Seems you run this in windows in an odd way. Show your code.
- 04-18-2010, 03:06 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
my original code is just this. I have a method which creates an html page, named VirtualPortal.html and then after finishing tha I have the line bellow in order to open the web page into the default browser.
Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "VirtualPortal.html");
Now when trying out your code, I just replaced the line above with
String[] browsers = { "firefox", "opera", "konqueror", "epiphany",
"mozilla", "netscape" };
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++) {
if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) {
browser = browsers[count];
}
}
if (browser == null) {
throw new Exception("Could not find web browser");
}
else {
Runtime.getRuntime().exec(new String[]{browser, "VirtualPortal.html"});
}
thanks
- 04-18-2010, 03:29 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
'which' is a Unix/Linux command; you won't find it in a default Windows installation.
kind regards,
Jos
- 04-18-2010, 05:01 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-18-2010, 05:15 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@OP, validate when you execute the code. Because the complete code not compatible with a one OS, different segments with different OSs. You can do simple validation sa such.
Java Code:String osName = System.getProperty("os.name"); if (osName.startsWith("Windows")){ Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); } else{ String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; String browser = null; for (int count = 0; count < browsers.length && browser == null; count++) { if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) { browser = browsers[count]; } } if (browser == null) { throw new Exception("Could not find web browser"); } else { Runtime.getRuntime().exec(new String[]{browser, url}); } }
- 04-18-2010, 05:49 PM #10
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
When you use Runtime.exec, you are outside the protective cover of portability you usually have from Java. There is no alternative to writing code to deal with every quirk of every OS when you do this.
If the web pages you're trying to display are fairly simple (just text and images, no flash, no javascript, etc.), then as an alternative to trying to bring up a browser, you could use a JEditorPane
- 04-18-2010, 05:58 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
- 04-19-2010, 03:59 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I agreed. But the way I use, deals the relevant DLLs and so on much use of me.
Similar Threads
-
Reg Java Opening
By krishnash in forum Jobs WantedReplies: 0Last Post: 02-07-2010, 11:58 AM -
[HELP]JButton opening a browser and opening a website[HELP]
By Learnin in forum AWT / SwingReplies: 4Last Post: 10-07-2009, 09:14 AM -
Opening in Java and .Net
By techinvo in forum Jobs OfferedReplies: 0Last Post: 03-22-2009, 07:24 AM -
Opening Text Files with Default System Editor
By Pesch in forum Advanced JavaReplies: 5Last Post: 10-08-2008, 06:17 PM -
opening jar files on pc prepared by mac
By willemjav in forum Advanced JavaReplies: 42Last Post: 09-27-2008, 05:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks