简体   繁体   中英

Run bat file from java code to get desired result in txt file - no can do :(

I have the following problem. I got a bat file that runs testcomplete test. After the test is finished in testcomplete, the app closes and exit code is passed back to the bat. Still in bat file i create a txt file called result and then depending on exit code i write to it successs, failure etc. When i run that bat file in Windows 7 i can see that test is being executed and after it's finished result.txt file appears with information i need. But when i simply run this same bat file from java code:

Process p1 = Runtime.getRuntime().exec(batch);

after the test is finished, file does not appear. Is there any way to get this to work fine? What should i change?

Script code is more less like that:

@ECHO OFF
"...\Bin\TestComplete.exe" "sometext.pjs" /r 
/p:sometext PathToApp="sometext.jnlp" Login=ads Password=ass  /t:"sometext|sometext" /exit 
IF ERRORLEVEL 3 GOTO CannotRun
IF ERRORLEVEL 2 GOTO Errors
IF ERRORLEVEL 1 GOTO Warnings
IF ERRORLEVEL 0 GOTO Success

:CannotRun
ECHO The script cannot be run >> "result.txt"
GOTO End

:Errors
ECHO There are errors >> "result.txt"
GOTO End

:Warnings
ECHO There are warnings >> "result.txt"
GOTO End

:Success
ECHO No errors >> "result.txt"
GOTO End

:End

我猜想您需要通过使用exec的重载版本来指定工作目录:

exec(String command, String[] envp, File dir)

Another thing is, that you should always read the InputStream of the process. If you don't the process might hang.

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