Results 1 to 12 of 12
  1. #1
    robby14 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default 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?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Just to guide you,

    Java Code:
    Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "VirtualPortal.html");
    works on Windows only.

    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});
                    }

  4. #4
    robby14 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default

    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

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you show me your complete code. Seems you run this in windows in an odd way. Show your code.

  6. #6
    robby14 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default

    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

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,530
    Blog Entries
    7
    Rep Power
    20

    Default

    'which' is a Unix/Linux command; you won't find it in a default Windows installation.

    kind regards,

    Jos

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    'which' is a Unix/Linux command; you won't find it in a default Windows installation.
    That's why I wonder. He just copy-paste the code and run it n Windows. I thouth he's just validate atleast on his code.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    @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});
                    }
                }

  10. #10
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,530
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by toadaly View Post
    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
    Or, (even better imho) is using the Desktop class and let it figure out what default browser is installed on your system and be done with it.

    kind regards,

    Jos

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Reg Java Opening
    By krishnash in forum Jobs Wanted
    Replies: 0
    Last Post: 02-07-2010, 11:58 AM
  2. Replies: 4
    Last Post: 10-07-2009, 09:14 AM
  3. Opening in Java and .Net
    By techinvo in forum Jobs Offered
    Replies: 0
    Last Post: 03-22-2009, 07:24 AM
  4. Opening Text Files with Default System Editor
    By Pesch in forum Advanced Java
    Replies: 5
    Last Post: 10-08-2008, 06:17 PM
  5. opening jar files on pc prepared by mac
    By willemjav in forum Advanced Java
    Replies: 42
    Last Post: 09-27-2008, 05:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •