I want to open a file within my java program but I can't get it to work. I would like my program to work on all platforms but especially in ubuntu.
Here is what I have:
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();
}
}
I was trying to open the file: /home/user/IdeaProjects/androidresourcehelper/test.xls
And I got the exception:
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
I wonder how I could specify a program to open EXCEL file. The ideal would be to get a list of programs available on system to open this type of files for example if windows excel is available to open with that but on if we are on ubuntu for example to use open office and on Mac use something else. Where could I find information on how to do this?
I managed to open an XML file:
Runtime.getRuntime().exec(new String[]{"gedit","strings.xml});
but that would only work if gedit is installed and I am not sure it would work on windows. For now I only need to open xls and xml files. Thanks in advance
EDIT:
Thanks to Peter's hint and this link: How do I programmatically determine operating system in Java?
I have managed to create this temporary solution:
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});
}
}