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.