I have the following method that is supposed to open the file that is passed in the associated program. It works for most files but if I pass any video or music file it prints java.io.IOException: Failed to open file:/C:/8GB/Bruno%20Mars%20-%20It%20will%20rain.mp3. Error message: Access is denied. (for example) and doesn’t open the file, even though it is associated with Windows Media Player!
private static void openFileinAssociate(File toOpen){
// Open it using the operating system/environment
Desktop environment = Desktop.getDesktop();
if(toOpen.exists()){
if(!toOpen.isDirectory())
try{
environment.open(new File(toOpen.getAbsolutePath()));
}catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "An error occured whilst trying to open the file.", applicationName, JOptionPane.ERROR_MESSAGE);
}
}
}
AND here is the rest of the stack trace as requested. I can really post anymore of the actual code though, sorry.
at sun.awt.windows.WDesktopPeer.ShellExecute(WDesktopPeer.java:77)
at sun.awt.windows.WDesktopPeer.open(WDesktopPeer.java:54)
at java.awt.Desktop.open(Desktop.java:272)
at main.GraphicUI.openFileinAssociate(GraphicUI.java:1930)
at main.GraphicUI.access$12(GraphicUI.java:1924)
at main.GraphicUI$17.mouseReleased(GraphicUI.java:837)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
I’m using Windows 7 and I have the latest version of Java…
The documentation of
Desktop.open(File)states, that:Try associating another application with the file in question and see if that works. Also, check the “Windows Event Log” to see if the WMP might be crashing when it’s opened.
An alternative (which will work at least on Windows) is to use the
Runtime.exec(String)-method and thestart [file]-command to start the given file with it’s associated application.Note: When your path has spaces in it, put the path in double quotes (need to be escaped in the String). Also, since the backslash is the escaping character for strings, it needs to be escaped, too.
Also note that this method will open the “Choose appropriate application”-dialog, when there is no application assigned for the given file-type.