1

I have a JAVA project in which I want to display the MySQL databases. I have written 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 gives me this error:

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

What should be done? thanks...

5
  • 2
    You aren't executing show within mysql, you're executing it as a new command line command. Commented Sep 25, 2013 at 13:28
  • I'm unsure, but try getting the outpustream from the mysql process and writing to that. Commented Sep 25, 2013 at 13:29
  • See answers of this SO question post, it shows how to get mysql database names using java. Commented Sep 25, 2013 at 13:34
  • 1
    show databases can be run through JDBC as well. Why do you want to run that as an external process? Commented Sep 25, 2013 at 13:35
  • add fully qualified path for the mysql.exe for process Commented Sep 25, 2013 at 13:38

2 Answers 2

4

Better to pass String object in exec method like:

Runtime.getRuntime().exec(  new String [] {"mysql", "-u", "root", "-pmanager", "-e", "show databases"} )
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.