1

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

4

2 回答 2

2

国家的文件Desktop.open(File)即:

抛出: IOException - 如果指定文件没有关联的应用程序关联的应用程序启动失败

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


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

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

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

另请注意,当没有为给定文件类型分配应用程序时,此方法将打开“选择适当的应用程序”对话框。

于 2012-08-07T15:01:11.587 回答
1

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

Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \"" + StringEscapeUtils.escapeJava(f.getAbsolutePath()) + "\"");
于 2014-12-05T11:12:42.273 回答