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?