我想在我的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文件的程序。理想的情况是获取系统上可用的程序列表以打开这种类型的文件,例如,如果windows excel可用于打开,但是如果我们在ubuntu上使用开放式办公室而在Mac上使用其他东西。我在哪里可以找到有关如何执行此操作的信息?
我设法打开了一个XML文件:
Runtime.getRuntime().exec(new String[]{"gedit","strings.xml});
但这只有在安装了gedit时才有效,我不确定它是否适用于Windows。 现在我只需要打开xls和xml文件。 提前致谢
编辑:
感谢彼得的暗示和这个链接: How do I programmatically determine operating system in Java?
我设法创建了这个临时解决方案:
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});
}
}
答案 0 :(得分:5)
Desktop.open()仅在系统知道默认为该文件类型打开哪个应用程序时才有效。如果设置不正确,则不能通过Java修复此问题。您可以通过猜测应该使用哪个程序来解决它,但是真正的解决方法是正确设置您的操作系统。