Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - IOException: Failed to open file: Access denied

I've been developing some software in Java that'll be distributed for use with Windows or MacOS. I'm programming on an iMac at the moment (because of the Apple specific requirements). The following method drags a helpfile (.pdf) out of resources and copies it locally so that it can be displayed on the local machine.

private void mHelpActionPerformed(java.awt.event.ActionEvent evt) {                                      
    String sourceFile = "resources/BRC2Help.pdf";
    String destFile = "brc2help.pdf";
    File f = new File (destFile);
    if (!f.exists()) {
            try {
            URL url = ClassLoader.getSystemResource(sourceFile) ;
            InputStream is = url.openStream();
            OutputStream os = new FileOutputStream(destFile);
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }
            is.close();
            os.close();
            System.out.printf("loaded pdf file from resources: %d bytes\n",f.length());
            } catch (IOException ex) {System.out.printf("loadPDF: %s\n",ex);}
    } else {
        System.out.printf("%s exists: %d bytes\n",destFile,f.length());
    }

    try {
        if (f.exists()) {
                f.setReadable(true);
                if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(f);
            } else {
                System.out.println("Awt Desktop is not supported!");
            }
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this,
                String.format("%s\n",ex),
                "Helpfile Problem?",
                JOptionPane.ERROR_MESSAGE);
    }
}                                     

This all works well enough on the Mac but gives an IOException on Windows 7:

java.io.IOException: Failed to open file:/C:/Program%20Files/BRC2/brc2help.pdf. Error message: Access denied.

Any idea what's going wrong here ? I've tried copying the .pdf file to other locations but the result is the same.

like image 259
user1494618 Avatar asked Aug 04 '26 18:08

user1494618


1 Answers

What is the working directory for the program when running on Windows? It is possible that the user context running the program doesn't have rights to write to c:\Program Files\.

You did not specify the path to the file so my assumption is that c:\program files\brc\ is the working directory while running the program. Since Windows Vista, you need to have full administration rights to be able to write to the Program Files and other directories.

Updated: 7/1/2012

Ok, I was able to stub out your program and run it as a main class and was able to have Desktop.getDesktop().open(f); open a PDF file on Windows 7. I manually put a PDF file into the bin directory where Eclipse compiled my class file to.

I now need to try moving the class file and pdf into a subdirectory of c:\program files\ and see if I get the access denied exception you receive.

Hum, I was able to get Desktop.getDesktop().open(f); open the PDF file when it was in c:\program files\test\, see my output below:

C:\Program Files\test>"\Program Files (x86)\Java\jre7\bin\java.exe" MyTestClass
brc2help.pdf exists: 943123 bytes

C:\Program Files\test>dir
 Volume in drive C is OS
 Volume Serial Number is 2035-793F

 Directory of C:\Program Files\test

07/01/2012  10:25 PM    <DIR>          .
07/01/2012  10:25 PM    <DIR>          ..
07/01/2012  09:55 PM           943,123 BRC2Help.pdf
07/01/2012  09:57 PM             2,391 MyTestClass.class
               2 File(s)        945,514 bytes
               2 Dir(s)  567,516,254,208 bytes free

What JRE are you using? Mine is java version "1.7.0_01" Java(TM) SE Runtime Environment (build 1.7.0_01-b08)

like image 195
HeatfanJohn Avatar answered Aug 07 '26 09:08

HeatfanJohn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!