简体   繁体   English

在 java(Windows 7)中使用桌面打开文件时出错

[英]error opening files with Desktop in java (windows 7)

I created a program that generates an excel spreadsheet (.xls), then asks if the user wants to open it immediately (if so, it uses java.awt.Desktops open() command to do so).我创建了一个程序,它生成一个 excel 电子表格 (.xls),然后询问用户是否要立即打开它(如果是,它使用 java.awt.Desktops open() 命令来执行此操作)。 This is working fine in windows xp, but when I tried with windows 7 it didn't work.这在 windows xp 中运行良好,但是当我尝试使用 windows 7 时它不起作用。 Below is a sample of my code...下面是我的代码示例...

Desktop myDesk = null;

//if printed to file successfully and java.awt.Desktop is supported
if (printed && Desktop.isDesktopSupported())
{   
    myDesk = Desktop.getDesktop();

    if (myDesk.isSupported(Desktop.Action.OPEN))
    {
         //ask to open file
        int openFile = JOptionPane.showConfirmDialog(null, "File successfully 
                          created.\nWould you like the excel file to open?", 
                          "open file?", JOptionPane.YES_NO_OPTION);

        //try to open file
        if (openFile == JOptionPane.YES_OPTION)
        {
            try { myDesk.open(myFile); }
            catch (IOException e){ JOptionPane.showMessageDialog(null, "Problem 
                                    opening file automatically, please open it
                                    yourself.", "Error", JOptionPane.ERROR_MESSAGE); }
        }
    }
}

On windows 7 this successfully prints to file, it shows the openFile dialogue, then shows the error dialogue.在 windows 7 上成功打印到文件,它显示 openFile 对话框,然后显示错误对话框。 This shouldn't happen, as in order to get to the openFile dialogue Desktop and Desktop.open() should both be supported.这不应该发生,因为为了进入 openFile 对话 Desktop 和 Desktop.open() 都应该被支持。 It could possibly have something to do with trying to open a ".xls" file instead of ".xlsx" file, but excel should still be set as default for either file type...它可能与尝试打开“.xls”文件而不是“.xlsx”文件有关,但 excel 仍应设置为任一文件类型的默认值...

So any ideas about why this is happening?那么关于为什么会发生这种情况的任何想法? And either how to fix it or if there's an alternate way to open a file that works better universally?以及如何修复它,或者是否有另一种方法可以打开一个更通用的文件?

This sounds like a standard Vista/7 UAC problem.这听起来像是标准的 Vista/7 UAC 问题。 You might want to try turning of User Account Control (UAC) in Control Panel->User Accounts->Turn User Account Control on or off.您可能想尝试在控制面板->用户帐户->打开或关闭用户帐户控制中打开用户帐户控制 (UAC)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM