0

I have a method that checks if a file exists then it deletes the file but will always create a new file (Jar file). I am using the org.eclipse.core.resources.IFile class for create and delete methods.

The code looks like this:

if(file != null){
   if(file.exists()){
       file.delete(true, monitor);
       system.out.println("File deleted");
     }
     system.out.println("File creation starting");
     file.create(stream, true, null);
     system.out.println("File created");
   }

Problem: When the jar file is existing, it calls the delete method but fails to call the other methods like the logs and the create method. But when I debug the whole process step by step it succeeds.

Example of scenarios for this issue

Scenario 1: Jar file is not in folder. It doesn't call the delete method. Calls the create method only. Build is successful.

Scenario 2: Jar file is already in the folder. It calls the delete method. Continues with the program without calling the create method or the sysout logs. Build fails.

Scenario 3: Jar file is already in the folder. Debug mode step by step. It calls the delete method and calls the create method. Build is successful.

May I know what am I missing? I tried to put a wait after the delete method and it succeeds but I don't think its the best solution for this problem.

ice
  • 141
  • 2
  • 12
  • Do you get any exception? – Nemanja Jun 18 '21 at 22:55
  • No i dont get any exception. It just skips the create method if delete method is called. – ice Jun 19 '21 at 04:11
  • The only way the create could possibly be skipped after the delete is because of a exception. Check the .log file in the workspace .metadata directory to see if one is being logged. – greg-449 Jun 19 '21 at 06:11
  • Hi greg, I already checked the .log file in the metadata directory but there are no exceptions/errors logged related to IFile. – ice Jun 19 '21 at 07:59
  • Are you looking in the correct metadata - if you are running this from within Eclipse it will be in the test workspace, not the main Eclipse workspace. Otherwise use the Eclipse debugger and step through the code. – greg-449 Jun 19 '21 at 08:47
  • Yes its the correct workspace. Also as I said in the post, if I debug it step by step, it calls the delete then the create methods without any problems. – ice Jun 19 '21 at 13:08
  • 1
    You could try surrounding this with a try-catch block and catching `Throwable` which should catch whatever is being thrown. – greg-449 Jun 19 '21 at 18:57

0 Answers0