Java - Desktop.open()不会打开视频或音乐

时间:2012-08-07 11:47:34

标签: java file windows-7 desktop

我有以下方法应该打开在关联程序中传递的文件。它适用于大多数文件,但如果我传递任何视频或音乐文件,它会打印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 ...

2 个答案:

答案 0 :(得分:2)

Desktop.open(File)个州documentation,即:

  

抛出: IOException - 如果指定的文件没有关联的应用程序,则关联的应用程序无法启动

尝试将其他应用程序与相关文件相关联,看看是否有效。此外,请检查“Windows事件日志”以查看WMP在打开时是否可能崩溃。


另一种选择(至少在Windows上可以使用)是使用Runtime.exec(String) - 方法和start [file] - 命令来启动给定文件及其关联的应用程序。

Runtime.exec("start \"C:\\Your\\Path with spaces\\file.whatever\"");

注意:当您的路径中包含空格时,请将路径放在双引号中(需要在字符串中进行转义)。此外,由于反斜杠是字符串的转义字符,因此也需要进行转义。

还要注意当没有为给定文件类型分配应用程序时,此方法将打开“选择适当的应用程序”-dialog。

答案 1 :(得分:0)

我遇到了同样的问题。不知道原因,但找到了解决方案。

Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \"" + StringEscapeUtils.escapeJava(f.getAbsolutePath()) + "\"");