• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File Not Found Error

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the error always occur after a successful read/write operation? If so, are you closing the file?

synchronized makes sure that 2 calls to the synchronized method are not in the method at the same time. It does not make sure that the method is always called before some event.

For example, if you have 2 threads running and they both call createBatFile, one of the calling methods is blocked until the other has returned from the method and the lock released.
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your response David. Perhaps this will make things clearer ...

After, the .bat file is created, three other methods run - one to apply a CHMOD to the .bat file, one to create a string for the .bat command, and a third to actually run the command. The third method is that in which the error sometimes occurs because the .bat file is not found. This method is shown below followed by log statements that are generated by this method.


/**** This method runs the .bat file after checking whether it exists ***/
synchronized void runBATfile(String batCommand) throws NotesException, IOException, InterruptedException {

try {
Runnable runnable = new BasicThread();
Thread thread = new Thread(runnable);
thread.start();
for (int i = 0; i < 20; i++) {
boolean exists = (new File(batFileName)).exists();
if (exists) {
LogAccessor.getLogger().logAction("batFileName
exists in runBATfile - " + batFileName);
break;
} else {
LogAccessor.getLogger().logAction("batFileName
does not exist in runBATfile - " + batFileName);
sleep(1000);
}
}

Process batChild = Runtime.getRuntime().exec
(batCommand);
batChild.waitFor();
}
catch(Exception e) {
LogAccessor.getLogger().logAction("agent failed.");
LogAccessor.getLogger().logError(e);
}
}

______________________________
The log statements (in order) are as follows:
17: [15:33:38] - batFileName exists in runBATfile -
US6C*73V97H-23.bat
18: [15:33:38] - agent failed.
19: [15:33:38] - java.io.IOException: US6C*73V97H-23.bat:
not found

______________________________

Note that each run generates a unique .bat file name.

What's not apparent is why this code occasionally generates a 'file not found' error after it was previously confirmed by the code that the .bat file exists.

Are you suggesting that each .bat file should be explicitly closed before it is run and that the failure to do so might be the cause of the error?

Thanks again.
[ June 06, 2007: Message edited by: Michael Scott ]
 
Michael Scott
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It turns out this was a permissions issue. The code was applying a 'chmod' to the .bat file, but apparently this process did not always complete on time. Adding the line below with the 'waitFor' method seemed to resolve this.

Process chmodChild = Runtime.getRuntime().exec(chmodCommand);
chmodChild.waitFor();


Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic