Java与另一个程序打开文件

我想在我的Java程序中打开一个文件,但我不能得到它的工作。 我希望我的程序可以在所有平台上运行,尤其是在Ubuntu上。

这是我有什么:

private void openFileChoser() { JFileChooser chooser = new JFileChooser("."); int returnVal = chooser.showOpenDialog(getParent()); if(returnVal == JFileChooser.APPROVE_OPTION) { openFile(chooser.getSelectedFile().getAbsolutePath()); } else { System.out.println("Aucun fichier choisi"); } } /*** * ouverture du fichier : * @param name */ private void openFile(String name) { System.out.println("Ouverture du fichier: " + name); try { Runtime.getRuntime().exec(new String[]{name}); //Desktop.getDesktop().open(new File(name)); //doesn't work on ubuntu 13.04 } catch (Exception e) { System.err.println("Erreur d'ouverture du fichier : "+name); e.printStackTrace(); } } 

我试图打开文件:/home/user/IdeaProjects/androidresourcehelper/test.xls

我得到了一个例外:

 java.io.IOException: Cannot run program "/home/user/IdeaProjects/androidresourcehelper/test.xls": error=13, Permission denied at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041) at java.lang.Runtime.exec(Runtime.java:617) at java.lang.Runtime.exec(Runtime.java:485) at com.vaxapp.android.ressources.parser.AppContentPane.openFile(AppContentPane.java:118) at com.vaxapp.android.ressources.parser.AppContentPane.openFileChoser(AppContentPane.java:104) at com.vaxapp.android.ressources.parser.AppContentPane.actionPerformed(AppContentPane.java:96) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289) 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:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) 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:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) Caused by: java.io.IOException: error=13, Permission denied at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.<init>(UNIXProcess.java:135) at java.lang.ProcessImpl.start(ProcessImpl.java:130) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022) ... 42 more 

我不知道如何指定一个程序来打开EXCEL文件。 理想的做法是获得系统上可用的程序列表来打开这种types的文件,例如,如果windows excel可以打开,但如果我们在U​​buntu上,例如使用开放式办公,在Mac上使用其他的东西。 我在哪里可以find如何做到这一点的信息?

我设法打开一个XML文件:

 Runtime.getRuntime().exec(new String[]{"gedit","strings.xml}); 

但是这只会在gedit安装时起作用,而且我不确定它会在Windows上工作。 现在我只需要打开xls和xml文件。 提前致谢

编辑:

感谢Peter的提示和这个链接: 如何以编程方式确定Java中的操作系统?

我设法创造了这个临时解决scheme:

  private void openFile(String path) { System.out.println("Ouverture du fichier: " + path); try { Tools.OsType osType = Tools.getOsType(); if (osType == Tools.OsType.LINUX) { Tools.FileType fileType = Tools.getFileType(path); if(fileType == Tools.FileType.XML) { Runtime.getRuntime().exec(new String[]{"gedit",path}); } else if(fileType==Tools.FileType.CALC) { openXlFile(path); } else { System.err.println("Extension inconnue"); } } else { Desktop.getDesktop().open(new File(path)); } } catch (Exception e) { System.err.println("Erreur d'ouverture du fichier : "+path); e.printStackTrace(); } } //localc == libre office //oocalc == open office private void openXlFile(String path) throws IOException { try { Runtime.getRuntime().exec(new String[]{"localc",path}); } catch (Exception e) { Runtime.getRuntime().exec(new String[]{"oocalc",path}); } } 

Desktop.open()仅在系统知道默认打开哪个应用程序的情况下适用于该文件types。 如果没有正确设置,这不是你可以从Java修复的东西。 您可以通过猜测应该使用哪个程序来解决这个问题,但是真正的解决方法是正确设置您的操作系统。