简体   繁体   中英

Running batch file from java causes hang, but not when running it normally

I am running a batch file which runs a battle in Robocode from within java, and then collecting the results of the battle. The code I am using to run the batch file is:

Process p = Runtime.getRuntime().exec("cmd /c C:\\Users\\Joel\\Documents\\ver5Files\\battle.bat");
p.waitFor();

The contents of my batch file are:

cd "C:\robocode"
java -DNOSECURITY=true -Xmx512M -Dsun.io.useCanonCaches=false -cp libs/robocode.jar robocode.Robocode -nodisplay -battle battles\DT_battle.battle -results resultsv5.txt
exit

This works fine if I run a battle with less than 35 rounds in robocode, however when I try and run one with 35 or more battles the code hangs and never returns to the java code.

I investigated further by running modifying the batch file so that it ran with a display, so I could watch the battles and find out where it was hanging. Once the 35th battle is done, it enters the "Round 35 cleaning up" step, and basically gets hung up there. The program isnt frozen or anything, it just wont initialize the next battle.

I have discussed it with someone who is working on a similar project and they are not having these issues, nor does our code differ significantly for this part. I have tried running it on two different machines, but get the same result on both.

Any advice?

Try to use /k instead of /c and tell me if it works.

Well, if you look at cmd.exe commands you will see it has many options. Some of them are:

/C     Run Command and then terminate

/K     Run Command and then return to the CMD prompt.
          This is useful for testing, to examine variables

You may find more options here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM