I have code with a synchronized method which creates a .bat file that's used later in the code. Sometimes, when the code runs, it generates an error which says that the .bat file could not be found. However, a logging statement just prior to when the missing .bat file error occurs confirms that the file exists. Furthermore, if I go to a command prompt, I can verify that the .bat file was in fact created within the same minute that the error occurred.
This suggests that there might be a
thread or timing issue that occasionally results in the .bat file not being created on time. I assumed that if the method which creates the file is synchronized this would ensure that the file is created before any other code runs. Is this an incorrect assumption?
The synchronized method that creates the .bat file is as follows. synchronized String createBATfile(String delRef) throws NotesException, IOException {
StringBuffer batFileNameBuffer = new StringBuffer(delRef);
batFileNameBuffer.append
(".bat");
String batFileName = batFileNameBuffer.toString();
File batFile = new File (batFileName);
// Create a file if it does not exist;
try {
boolean batCreateSuccess = batFile.createNewFile();
}
catch(Exception e) {
LogAccessor.getLogger().logAction(".bat file failed to be created.");
LogAccessor.getLogger().logError(e);
}
return batFileName;
} Here's the code that confirms that the .bat file exists just prior to when the missing file error occurs. boolean exists = (new File(batFileName)).exists();
if (exists) {
LogAccessor.getLogger().logAction("batFileName exists" + batFileName);
} else {
LogAccessor.getLogger().logAction("batFileName doesn't exist" +
batFileName);
} Thanks.