Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Openning a file in Native Application not Working

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Openning a file in Native Application not Working

    First post to this forum.

    I'm using Desktop open() to open a file. I have identical code in two places in my program one works and the other doesn't. The excerpt below doesn't work; but this is a copy+paste of code that does work. Code is duplicated for two different JPanels one handles Photo files and this one does Media files.

                    try {
                        path = myDbI.getSingleString(query);
                        myLog.debug("path = " + path);
                        fileToOpen = path + fs + selectedLocalDir + fs + selectedFileName;
                        myLog.debug("Attempt to open file using native Application; File To Open: " + fileToOpen);
                        File file = new File(fileToOpen);
     
                        if (file.canRead() && file.isFile()) {
                            myLog.debug("file absolute path: " + file.getAbsolutePath());
                            Desktop dt = Desktop.getDesktop();
                            dt.open(file);
                        } else {
                            myLog.debug("MediaFilePanel() - cannot Open file.");
                            JOptionPane.showMessageDialog(currentFrame, "Can Not Open this file.");
                        }
                    } catch (SQLException se) {
                        myLog.warn("MediaFilePanel() - SQL Exception fetching LPATH from FILELOC: " + se);
                    } catch (IOException ioe) {
                        myLog.warn("MediaFilePanel() - IO Exception opening file: " + ioe);
                    }

    I log (below) the path to the file I want opened
    I hit the IOException with the error reported:

    DEBUG  [ui.MediaFilePanel] Attempt to open file using native Application; File To Open: C:\Documents and Settings\Jim\My Documents\Genealogy\MEDIA\Movie-1.wmv 
    DEBUG  [ui.MediaFilePanel] file absolute path: C:\Documents and Settings\Jim\My Documents\Genealogy\MEDIA\Movie-1.wmv 
    WARN   [ui.MediaFilePanel] MediaFilePanel() - IO Exception opening file: java.io.IOException: Failed to open file:/C:/Documents%20and%20Settings/Jim/My%20Documents/Genealogy/MEDIA/Movie-1.wmv. Error message: The parameter is incorrect.

    Notice the path in the Exception message for the file that 'Failed to open'.
    It appears that the path to the file is getting miss-read in the statement:
    dt.open(file);

    Elsewhere in my program the code works for files of type JPG. Here is an extract from my log file:
    DEBUG  [dbi.DbI] getSingleString() - returning result: C:\Documents and Settings\Jim\My Documents\Genealogy 
    DEBUG  [ui.PhotoFilePanel] Attempt to open file using native Applcation; File To Open: C:\Documents and Settings\Jim\My Documents\Genealogy\HEADSTONES\Jones, Annie Mea-21#K2_Grave.jpg

    My problem does not appear to be related to the file type - the path to the file it is trying to open looks like it is getting screwed up.

    Any ideas of what I can try to isolate the cause? And of course fix this?

    If more information is needed - just ask.

    Thanks,
    Jim


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Openning a file in Native Application not Working

    Hello wagb278. Welcome to the Java Programming Forums.

    I think we may need to see the rest of your code to be able to help you with this.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,370
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Openning a file in Native Application not Working

    It doesn't look to me like the path is getting screwed up...%20 is hex for a space, so the spaces are only being replaced with hex (spaces in file paths can screw things up). My suggestion would be to test the path by hard-coding it and debug through there...as well as making sure there is default application to open the file (see the API for Desktop...Desktop.open throws an IOException if there is no associated application to open the file)

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Openning a file in Native Application not Working

    Thanks for the replies.

    I will make a little test with a hard coded path. The path screw up I was concerned with are the direction of the file separator characters and the leading slash (first char in Exception, before the 'C:'). I am testing this on a Windows machine. On this machine I can double-click the file in question and the file opens in the correct native application, so I was assuming the system has an application registered for the file open(). I will research that API more.

    Thanks again, at least I have something to try. I was at a loss for how to proceed. I will report back with test results.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Openning a file in Native Application not Working

    I found an article that indicates there is no way for java to determine if there is an application associated with the file extension. I did verify that there are applications associated with the file extensions involved. My little test give the same results. Code below. However, once the program crashed on the *.wmv file but that is not repeatable - usually I just get the IOException with the message "The parameter is incorrect"


    package awtdesktoptest;
     
    import java.awt.Desktop;
    import java.io.File;
    import java.io.IOException;
     
    /**
     *
     * @author Jim
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            boolean openSupported = false;
            String jpgPath = "C:\\Documents and Settings\\Jim\\My Documents\\Genealogy\\SOURCES\\5-020.jpg";
            String wmvPath = "C:\\Documents and Settings\\Jim\\My Documents\\Genealogy\\MEDIA\\Movie-1.wmv";
            String wavPath = "C:\\Documents and Settings\\Jim\\My Documents\\Genealogy\\MEDIA\\Audio1.wav";
     
            Desktop dt = null;
            // Before more Desktop API is used, first check 
            // whether the API is supported by this particular 
            // virtual machine (VM) on this particular host.
            if (Desktop.isDesktopSupported()) {
                dt = Desktop.getDesktop();
     
                if (dt.isSupported(Desktop.Action.OPEN)) {
                    openSupported = true;
                }
            }
     
            System.out.println("Desktop supports OPEN: " + openSupported);
            if (openSupported) {
                File file = new File(wmvPath);
                try {
                    System.out.println("File to Open: " + file.getAbsolutePath());
                    dt.open(file);
                } catch (NullPointerException ne) {
                    System.out.println("Null Exception: " + ne.getMessage());
                } catch (IllegalArgumentException ie) {
                    System.out.println("Illegal Argument Exception: " + ie.getMessage());
                } catch (IOException ioe) {
                    System.out.println("IO Exception: " + ioe.getMessage());
                } catch (SecurityException se) {
                    System.out.println("Security Exception: " + se.getMessage());
                }
            }
        }
     
    }

    I manually change the code to open the desired file in one of the Strings. The jpgPath works but the other two fail. I tried removing the "C:" at the beginning of one of the Strings, but that didn't change the absolute path, nor results of the experiment.
    Log results:

    Desktop supports OPEN: true
    File to Open: C:\Documents and Settings\Jim\My Documents\Genealogy\MEDIA\Movie-1.wmv
    IO Exception: Failed to open file:/C:/Documents%20and%20Settings/Jim/My%20Documents/Genealogy/MEDIA/Movie-1.wmv. Error message: The parameter is incorrect.

    The fact that I got a crash once leads me to believe there is an error in the awt.Desktop code - but I find that difficult to accept. The Crash report created a log which I saved and asked if I want to open a bug report. I haven't done that.

    I set a break point at the open(file) and stepped through that logic and the file opened as I wanted. If I do not step through the open() but just continue - I get the file IO Exception. What gives?

Similar Threads

  1. Urgent - File exist nor working
    By Bagzli in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 2nd, 2011, 04:09 AM
  2. Jar Executable File Not Working
    By Kimimaru in forum Java Theory & Questions
    Replies: 6
    Last Post: October 15th, 2010, 10:32 PM
  3. [SOLVED] Help:File Tracking Application..
    By Cross`17 in forum Java Networking
    Replies: 4
    Last Post: April 20th, 2010, 08:32 AM
  4. [SOLVED] [help] the application file (.jad) for application does not appear to be the ....
    By ordinarypeople in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: April 4th, 2010, 04:50 AM
  5. Openning Image with java
    By shadihrr in forum Java Theory & Questions
    Replies: 3
    Last Post: February 3rd, 2010, 06:44 AM

Tags for this Thread