3

I am trying to automate some processes that were build in ancient times, for the sake of avoiding repetitive actions. It is required that the processes are started with one batch and stopped with another (this can not be changed btw).

So i made a commandline tool to do this (and many other repetitive stuff) and I have modelled a command that starts the 'startbatch' and a command that start the 'stopbatch'. Both commands work fine separatly (as I tested them separatly) but there seems to be a problem when i want execute them one after another (in the correct order ofcourse). I get the following error in new cmd.exe window:

The process cannot access the file because it is being used by another process.

the code that i am using to start the batches looks like this:

public void startBatchInDev(String company){
    String startBatchFolder = locations.getLocationFor("startbatch");

    try{
        Runtime runtime = Runtime.getRuntime();
        runtime.exec("cmd.exe /C cd \"" + startBatchFolder + "\" & start cmd.exe /k \"" + BATCHSTART + company.toLowerCase()+ "-dev"  + BATCH_SUFFIX + "\"");
    }
    catch(IOException ioe){
        ioe.printStackTrace();
    }
}

public void stopBatchInDev(String company){
    String startBatchFolder = locations.getLocationFor("startbatch");

    try{
        Runtime runtime = Runtime.getRuntime();
        runtime.exec("cmd.exe /C cd \"" + startBatchFolder + "\" & start cmd.exe /k \"" + BATCHSTOP + company.toLowerCase()+ "-dev"  + BATCH_SUFFIX + "\"");
    }
    catch(IOException ioe){
        ioe.printStackTrace();
    }
}

The names of the batchfiles are concatenated, but they are OK once the application is running.

The error message is quite clear, some file is locked and I can't access it because of it. Some googling confirms my suspicion, but I can't seem to find a solution for this. The hits in google are all about obvious uses of files, like an obvious shared resource. But in my case, i am not working on the same batch file. The stop and start batch are two different files. So I am actually starting to think that it might be the cmd.exe file that is being locked by windows...

So this question is actually two questions: - what is the exact cause of the described problem? - how do i programmatically fix this (if possible)?

thanks in advance!

4

1 回答 1

1

所以,基本上,蝙蝠不是那么好:-(我能够从java复制这个,但我也发现这个脚本:

@echo off
echo STOP
echo STOP >> E:\tmp\java\logfile.txt
C:\cygwin\bin\sleep.exe 1
echo STOP1 >> E:\tmp\java\logfile.txt
C:\cygwin\bin\sleep.exe 1
echo STOP2 >> E:\tmp\java\logfile.txt

当像这样运行两次时:

start test.bat && start test.bat

将失败并显示一条或多条消息,例如:

The process cannot access the file because it is being used by another process.

原因是“>>”重定向打开文件进行读/写访问,但只有 FILE_SHARE_READ 共享。如果两个不同的程序试图以这种方式打开文件,其中一个会失败。

因此,您不能同时运行两个不同的批处理文件并记录到同一个文件

于 2013-10-03T19:53:11.570 回答