1

hi I have a JAVA project in which I want to display the MySQL databases. i write this code:

try {
     String []command={"mysql -u root -pmanager","show databases"};
        Process p= Runtime.getRuntime().exec("mysql -u root -pmanager");
        Process p1= Runtime.getRuntime().exec("show databases");
        if (p.waitFor()==0){System.out.println("backup done...");}
        else{System.out.println("!!!");}
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

but it give me this error:

Cannot run program "show": CreateProcess error=2, The system cannot find the file specified

what it should be do? thanks...

Archer
  • 5,073
  • 8
  • 50
  • 96
ooo
  • 17
  • 1
  • 5

2 Answers2

4

Better to pass String object in exec method like:

Runtime.getRuntime().exec(  new String [] {"mysql", "-u", "root", "-pmanager", "-e", "show databases"} )
Ammar Ali
  • 692
  • 6
  • 20
Archer
  • 5,073
  • 8
  • 50
  • 96
0

You need to specify what is 'mysql' to command prompt... example - C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql

Tharaka Dilshan
  • 4,371
  • 3
  • 14
  • 28