我有以下方法应该打开在关联程序中传递的文件。它适用于大多数文件,但如果我传递任何视频或音乐文件,它会打印java.io.IOException: Failed to open file:/C:/8GB/Bruno%20Mars%20-%20It%20will%20rain.mp3. Error message: Access is denied.
(例如)并且不会打开文件,即使它与 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);
}
}
}
并且这是所要求的堆栈跟踪的其余部分。不过,我真的可以发布更多实际代码,抱歉。
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)
我正在使用 Windows 7,并且我拥有最新版本的 Java ...