从java代码运行bat文件以在txt文件中获得所需的结果 - 不能:(
我有以下问题。 我有一个运行 testcomplete 测试的 bat 文件。在 testcomplete 中完成测试后,应用程序将关闭,退出代码将传递回 bat。仍在bat文件中,我创建一个名为result的txt文件,然后根据退出代码向其写入成功、失败等。 当我在 Windows 7 中运行该 bat 文件时,我可以看到测试正在执行,完成后会出现 result.txt 文件,其中包含我需要的信息。 但是,当我简单地从 java 代码运行同一个 bat 文件时:
Process p1 = Runtime.getRuntime().exec(batch);
测试完成后,文件不会出现。有什么办法可以让它正常工作吗?我应该改变什么?
脚本代码不太像这样:
@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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一件事是,您应该始终读取进程的InputStream。如果不这样做,进程可能会挂起。
Another thing is, that you should always read the InputStream of the process. If you don't the process might hang.
我猜您需要使用
exec
的重载版本来指定您的工作目录:exec(字符串命令,细绳[] envp,文件目录)
I would guess that you need to specify your Working Directory, by using an overloaded version of
exec
:exec(String command, String[] envp, File dir)