从java代码运行bat文件以在txt文件中获得所需的结果 - 不能:(

发布于 2024-11-26 15:45:27 字数 932 浏览 1 评论 0原文

我有以下问题。 我有一个运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-12-03 15:45:28

另一件事是,您应该始终读取进程的InputStream。如果不这样做,进程可能会挂起。

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

酒浓于脸红 2024-12-03 15:45:27

我猜您需要使用 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文