0

I have written a bat file to install mysql. It works well when executing by double clicking but when it executed by java program it prompted and vanishes, nothing happens. Any suggestions?

this is my bat file:

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

this is my java code:

Runtime.getRuntime().exec("cmd /c start MySQL.bat");
Ruwanka De Silva
  • 3,555
  • 6
  • 35
  • 51
  • It will helps you : http://stackoverflow.com/questions/20118746/how-to-install-mysql-from-batch-file – Keval Trivedi Jul 10 '14 at 04:52
  • my bat files runs perfectly by itself and installs mysql, but not with java program, its the problem I am having. – Ruwanka De Silva Jul 10 '14 at 05:13
  • When launching commands through ... (say java in your use case) never be confident in environment variables. Use full paths for all commands (at least `MySQLInstanceConfig`) and spy (`echo "%pro%" > c:\accessible\path\spy.txt`) the other environment variables you use. – Serge Ballesta Jul 10 '14 at 06:20

2 Answers2

0

Assuming your bat file works outside of Java, just change this

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

to

Runtime.getRuntime().exec("MySQL.bat");
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

I suggest to try this one :

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();
Keval Trivedi
  • 1,290
  • 2
  • 13
  • 29