从Java code运行bat文件以获得所需结果txt文件 - 没有做不到:( [英] Run bat file from java code to get desired result in txt file - no can do :(

查看:144
本文介绍了从Java code运行bat文件以获得所需结果txt文件 - 没有做不到:(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。
我有一个运行testcomplete测试一个bat文件。试验在testcomplete完成后,应用程序关闭并退出code传递回蝙蝠。仍然在bat文件我创建一个txt文件名为结果,然后根据退出code我写它successs,故障等。
当我运行在Windows 7中的bat文件,我可以看到正在执行的测试,它的完成的Result.txt文件后,我需要的信息出现。
但是,当我只需运行在Java code同样的bat文件:

过程P1 =调用Runtime.getRuntime()执行(批);

测试完成

后,文件未出现。有什么办法得到这个工作的罚款?我应该改变什么?

脚本code更不像是:

  @ECHO OFF
... \\ BIN \\ TestComplete.exesometext.pjs/ R
/ P:sometext PathToApp =sometext.jnlp登录=广告=密码屁股/吨:sometext | SomeText则会/退出
IF ERRORLEVEL 3 GOTO CannotRun
IF ERRORLEVEL 2 GOTO错误
IF ERRORLEVEL 1 GOTO警告
IF ERRORLEVEL 0 GOTO成功:不能跑
ECHO脚本无法运行>> 的Result.txt
GOTO结束:错误
ECHO有错误>> 的Result.txt
GOTO结束:警告
ECHO有警告>> 的Result.txt
GOTO结束:成功
ECHO没有错误>> 的Result.txt
GOTO结束:结束


解决方案

我猜想,你需要指定工作目录,使用 EXEC的重载版本

<一个href=\"http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec%28java.lang.String,%20java.lang.String%5b%5d,%20java.io.File%29\"相对=nofollow> 执行exec(字符串命令,字符串[] envp,文件目录)

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

解决方案

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)

这篇关于从Java code运行bat文件以获得所需结果txt文件 - 没有做不到:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆