batch-filelockfile

lock file won't work with windows .bat file


I'm creating a lock file to prevent multiple instances of my program as seen here How to implement a single instance Java application?.

If I run my program from the command line it works fine, that is it starts my program and creates the lock file. When I try to run my program again it recognizes the lock file and does not start another instance, it passes command line arguments to the currently running process. That is what I want.

If I use a .bat file, the second time I run the .bat file I get this message "The process cannot access the file because it is being used by another process." That message is not from my program. The program does not run as expected. I find it odd that it gets to the point of my program to try and access the file but none of my print statements appear in my log file. I have log files for my program and I capture stdout from the .bat file.

Here it is

echo "START" >> C:\Users\Joe\Documents\NetBeansProjects\MyProgram\logs\myp.log
java -jar C:\Users\Joe\Documents\NetBeansProjects\MyProgram\target\MyProgram.jar %1  >> C:\Users\Joe\Documents\NetBeansProjects\MyProgram\logs\myp.log 2>&1

echo "FINISH"  >> C:\Users\Joe\Documents\NetBeansProjects\MyProgram\logs\myp.log

Solution

  • It seems the issue has nothing to do with the lock file I am using and everything to do with the log file I am writing to in my batch file.
    Not sure how to fix THAT issue yet but if I remove the writing of the log file from my batch file everything works fine.