Take the 2-minute tour ×

My Java application needs to connect to a Mysql database file which is present on disk.

For that, it needs to start a Mysql server and use the server to read the file. The problem is that I am not sure how to start the server from within my Java code, read the Mysql file, make modifications to it, and then stop the server.

Currently, I am using something like below, but I don't see a way to start the server from code.

MysqlDataSource dataSource = new MysqlDataSource();
Connection conn = dataSource.getConnection();

Using this, I run into

Exception in thread "main" java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

which I think is because the server is not up. How do I start the server from code?

share|improve this question
Were you able to run the app if mysql is started manually? Network errors could come from antivirus software/firewall etc –  Jayan Apr 24 '13 at 2:04
Which OS you use? –  Sai Ye Yan Naing Aye Apr 24 '13 at 2:08
@yan: yes, the app works if mysql server is running. –  Moeb Apr 24 '13 at 2:13
@Sai: I am using OSX but looking for a platform independent solution. –  Moeb Apr 24 '13 at 2:14
If you want platform independence then why not use Apache Derby instead of MySQL? –  Thorbjørn Ravn Andersen Apr 24 '13 at 3:09

5 Answers 5

  • Option 1 : Find out how to start mysql from command line. Then use ProcessBuilder to do that
  • Option 2: Use embeddable version of mysql , if that suits you : See details
share|improve this answer

Look into Runtime.exec(), which will allow you to start mySQL via console.


share|improve this answer
1) I would recommend the use of a ProcessBuilder instead of Runtime.exec() 2) Try to link to more up-to-date documentation in an answer. Right now, you are linking to Java 1.5 –  syb0rg Apr 24 '13 at 1:56
Fair enough. It's just the Java Doc link that came up first in Google, still works the same in 1.7. –  MarsAtomic Apr 24 '13 at 6:40

you need to run 'mysqld.exe' to start the mysql server. This file can be found in the 'bin' folder in the mysql main folder (Assuming that you are working in windows). In my case 'mysql-5.6.10-win32\bin\mysqld.exe' .

From Java if you want to do the same - I used the following code and it worked. Hope it helps.


String command = "~path~\\mysql-5.6.10-win32\\bin\\mysqld.exe";

    Process process = Runtime.getRuntime().exec(command);
catch (IOException e)
share|improve this answer
Runtime runTime = Runtime.getRuntime();
Process p = runTime.exec(String[] cmdarray, String[] envp, File dir);
share|improve this answer

Easiest way is to use JDBC all you need to do then is send queries to the server and interpret the results. A simple way to connect is below.

import java.sql.*;
Connection conn;
private void connect()
        conn = DriverManager.getConnection("jdbc:mysql://serverURL","user","pass);
        System.out.println("Connection Established");
    catch (Exception ex) 
        System.err.println("Unable to load the MySQL driver.");
share|improve this answer
This expects the server to be running. He needs to start it. –  Thorbjørn Ravn Andersen Apr 24 '13 at 3:11

Your Answer


