The question is published on by Tutorial Guruji team.
I have java desktop application where I have file upload and view feature. Here is my code for opening a file
public static boolean open(File file) { OSDetector osdetector = new OSDetector(); try { if (osdetector.isWindows()) { Runtime.getRuntime().exec(new String[]{"rundll32", "url.dll,FileProtocolHandler", file.getAbsolutePath()}); return true; } else if (osdetector.isLinux() || osdetector.isMac()) { Runtime.getRuntime().exec(new String[]{"/usr/bin/open", file.getAbsolutePath()}); return true; } else // Unknown OS, try with desktop if (Desktop.isDesktopSupported()) { Desktop.getDesktop().open(file); return true; } else { return false; } } catch (Exception e) { e.printStackTrace(System.err); return false; } }
This is perfectly working in MAC OS but when I am running in windows 7 PC it won’t open files. Following are the error messages;
Adobe reader error: “There was an error opening this document. This file is already open or in use by another application”
Windows Photo viewer error message: “Windows photo viewer can’t open this picture because the picture is being edited in another program”
Paint error message: “A sharing violation occurred while accessing …..”
Please help
Thank You
Answer
Windows can’t cope with the idea of two programs using a file at once, presumably due to its DOS single-user origins. Make sure that when you save the file, you close it before you call your open()
method.