1

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). This is working fine in windows xp, but when I tried with windows 7 it didn't work. 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. This shouldn't happen, as in order to get to the openFile dialogue Desktop and Desktop.open() should both be supported. 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...

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?

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
scaevity
  • 3,991
  • 13
  • 39
  • 54
  • [Javadoc](http://download.oracle.com/javase/6/docs/api/java/awt/Desktop.html#open(java.io.File)), to save a little time for those attempting to answer: "IOException - if the specified file has no associated application or the associated application fails to be launched". – Michael Myers Jun 02 '11 at 19:33
  • @michael-myers But excel is set as the associated application (if I double click on the file, it open in excel with no problems), so why would excel be failing to launch? – scaevity Jun 02 '11 at 19:56

1 Answers1

1

This sounds like a standard Vista/7 UAC problem. You might want to try turning of User Account Control (UAC) in Control Panel->User Accounts->Turn User Account Control on or off.

Mike_K
  • 9,010
  • 5
  • 20
  • 27
  • you mean I should have each user who uses my program manually change their computer settings before running the program? if so, that doesn't seem like a very simple solution. – scaevity Jun 02 '11 at 19:45
  • Well, windows vista/7 doesn't want applications opening other applications in essence. Have you tried running your application as Administrator in windows 7 (right click, run as administrator)? Even more important, why do you really need an app to open Excel for the user? – Joseph Jun 02 '11 at 20:14