当由java执行时,Bat文件没有按预期执行

时间:2014-07-10 04:47:03

标签: java mysql batch-file

我写了一个bat文件来安装mysql。它通过双击执行时效果很好但是当它由java程序执行时它会提示并消失,没有任何反应。有什么建议吗?

这是我的蝙蝠档案:

set cur=%cd%
set pro=%PROGRAMFILES(x86)%
pause
if exist "%pro%"  goto yes86
set pro=%PROGRAMFILES%

:yes86
if exist "%pro%\MySQL\MySQL Server 5.5\bin\mysql.exe" goto yesmysql

msiexec /i "mysql-5.5.13.msi" /passive INSTALLDIR="%pro%\MySQL\MySQL Server 5.5"

cd /d %pro%\MySQL\MySQL Server 5.5\bin

MySQLInstanceConfig.exe -i -q "-lC:\mysql_install_log.txt" "-nMySQL Server 5.5" "-       p%pro%\MySQL\MySQL Server 5.5" -v5.5.13 "-t%pro%\MySQL\MySQL Server 5.5\my-template.ini" "-c%pro%\MySQL\MySQL Server 5.5\my.ini" ServerType=DEVELOPMENT DatabaseType=MIXED ConnectionUsage=OLTP Port=3306 ServiceName=Mysql Charset=utf8 RootPassword=abc
:yesmysql

这是我的java代码:

Runtime.getRuntime().exec("cmd /c start MySQL.bat");

2 个答案:

答案 0 :(得分:0)

假设您的bat文件在Java之外工作,只需更改此

即可
Runtime.getRuntime().exec("cmd /c start MySQL.bat");

Runtime.getRuntime().exec("MySQL.bat");

答案 1 :(得分:0)

我建议试试这个:

List cmdAndArgs = Arrays.asList({"cmd", "/c", "MYSQL.bat"});
File dir = new File("C:/Program Files/MySQL/MySQL Server 5.5/bin/bat");

ProcessBuilder pb = new ProcessBuilder(cmdAndArgs);
pb.directory(new File(dir));
Process p = pb.start();