Inzimam Tariq IT Inzimam Tariq IT - 1 year ago 36
SQL Question

Status of mysql and apache services in Xampp using java (running/not running)

I'm writing a program that uses

for data fetching and data storage. On its first
it creates Database and all necessary table if program is being used for the first time.

In Later attempts it connect to that Database environment. I want to know how with
java code
can I check if
mysql service
apache service
is running or not. If services are not running I have to run those.

I have this piece of code to run services

1. Process process =
2. Process process =

But how can I get their Status (running / not running).

Answer Source

Use this code to this is not the best way But it is one of the way with which you can do that work. Check the code below

public boolean isServerUp(int port) {
        boolean isUp = false;
        try {
            Socket socket = new Socket("", port);
//            Socket socket = new Socket("localhost", port); <- also this
            // Server is up
            isUp = true;
        } catch (IOException e) {
            // Server is down
        return isUp;

you can also use localhost instead of
And can use this for checking the services

boolean isApacheRunning = f.isServerUp(82);
boolean isMySqlRunning = f.isServerUp(3306);
JOptionPane.showMessageDialog(null, "Is tomcat Running = " + isApacheRunning + "\nIs mysql Running = " + isMySqlRunning);
            if (!isMySqlRunning){
            if (!isApacheRunning){

f in the above code is reference of Functions class and RunMySQL(); & RunApache(); are methods to start services that contain code as in your question.