Java打开文件与另一个程序 [英] Java open file with another program

查看:85
本文介绍了Java打开文件与另一个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java程序中打开一个文件,但无法正常工作.我希望我的程序可以在所有平台上运行,尤其是在ubuntu中.

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.

这就是我所拥有的:

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

I was trying to open the file: /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上,例如使用Open Office,而在Mac上,则可以使用其他程序.在哪里可以找到有关如何执行此操作的信息?

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?

我设法打开一个XML文件:

I managed to open an XML file:

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

,但这仅在安装了gedit的情况下有效,并且我不确定它是否可以在Windows上运行.现在,我只需要打开xls和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

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

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});
    }
}

推荐答案

Desktop.open()仅在系统知道默认情况下针对该文件类型打开哪个应用程序时才起作用.如果未正确设置,则无法通过Java进行修复.您可以通过猜测是否应该使用哪个程序来解决该问题,但真正的解决方法是正确设置操作系统.

Desktop.open() only works if the system knows which application to open by default for that file type. If this is not setup correctly this is not something you can fix from Java. You can work around it by guess which program should be used, as you have done, but the real fix is to setup your OS correctly.

这篇关于Java打开文件与另一个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆