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.
Code Java:
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:
Code :
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:
Elsewhere in my program the code works for files of type JPG. Here is an extract from my log file:
Code :
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
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.
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)
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.
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"
Code :
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:
Code :
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?