Results 1 to 8 of 8
  1. #1
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Post Opening .html file

    Hi
    I want to open index.html file when I click on the menu item of the GUI, this file is in dist/javadoc/index.html of my GUI application project.
    I tried
    Java Code:
                    try {
     Desktop.getDesktop().open(new File("dist/javadoc/index.html"));  
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                        JOptionPane.showMessageDialog(null, "Can't open requested file.", "Error", JOptionPane.ERROR_MESSAGE);
                    }
    Here I didn't gave the full path because I thought it might give problem when this application jar file is shifted to next PC.
    I don't have much idea on File IO operation, so any help is appreciated.

    -Regards

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    What happens when you execute the code?
    For debugging, take the new File() out of the method call, create a File object and printout its absolute path to see if the path to the file is correct.

  3. #3
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What happens when you execute the code?
    When executed, I got error message saying file don't exist. So changed code and I did this
    Java Code:
     File file = new File("C:/Users/prajin/Documents/NetBeansProjects/MMS/dist/README.TXT");
                        System.out.println("");
                        System.out.println(file.canExecute());
                        System.out.println( file.getPath());
                        Desktop.getDesktop().open(file);
    I got it working,the README.txt is open, but still one issue is left.
    Using absolute path to open file is not what I want, because README.txt always have to in same directory.
    If I move the application to D: drive with README.TXT along, then absolute path won't work. What I'm looking is relative path, that will search the application directory and open readme.txt.

    -Regards

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

  5. #5
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Yup it's NetBeans project.
    But I got it working,
    Java Code:
     String currentDirectory = System.getProperty("user.dir");
                        System.out.println(currentDirectory);
                        File file = new File(currentDirectory+File.separator+"dist/README.TXT");
                        System.out.println(file.getName());
                        System.out.println( file.canExecute());
                        System.out.println(file.getPath());
                        Desktop.getDesktop().open(file);
    Now where ever application is shifted, file will open. Just I have to make sure that readme.txt file is inside application directory.

    Thanks for the help

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

    Default

    The things is this. Do you ever heard about relative paths and absolute paths we are talking about in almost all languages. You've access the file in wrong way in *.html case. Use of text file follows the complete file, where system can easily locate it.

  7. #7
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Yeah I have heard about absolute and relative path. And for *.html case, I was doing it wrong, I agree.

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

Similar Threads

  1. Error While Opening A PDF file
    By Cluster Storm in forum AWT / Swing
    Replies: 6
    Last Post: 06-07-2010, 07:03 PM
  2. Opening applet with .html file
    By Atriamax in forum New To Java
    Replies: 12
    Last Post: 09-13-2009, 07:26 AM
  3. Opening a custom proyect file
    By janpol1 in forum Eclipse
    Replies: 0
    Last Post: 02-04-2009, 03:29 PM
  4. How can I include a html file in html textarea?
    By surya_dks in forum New To Java
    Replies: 2
    Last Post: 10-04-2008, 08:20 AM
  5. Problem in opening a file
    By Raghav kv in forum New To Java
    Replies: 1
    Last Post: 08-11-2007, 02:55 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
  •