0

I want to backup a MYSQL table from remote server to my local server and I have to do this in my java application. I am trying to run the follwing query to do so:

C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump --opt --where='1 limit 10' -h a.b.c.d -u root -proot remoteDB remotetable|mysql -u root -pcanada localDB

Query runs absoulutely fine when i run it through commandline but when i run it through my java application it is not able to create any backup and strangely does not show any output also.Following is the code:

    public static void main(String[] args) {
    Process p;
    String s="C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump --opt " +
                "--where='1 limit 1'  -h a.b.c.d  -u root -proot remoteDB remoteTable|mysql -u root -pcanada " +
                "localDB";
    try {
        p = Runtime.getRuntime().exec(s );
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line=reader.readLine();

        while (line != null) {    
            System.out.println(line);
            line = reader.readLine();
        }
    }
    catch (InterruptedException e) {
        System.out.println("fsff"+e);
            e.printStackTrace();
    } catch (IOException e) {
        System.out.println("fsdaf"+e);
            e.printStackTrace();
    }

}

Please could anyone suggest why I am not run it through my java application.Thanks in advance :)

2 Answers 2

1

Runtime.exec is not a shell. It starts one process only, and doesn't handle input and output stream redirection, so command1 | command2 won't work.

As a workaround you can write the command into a .cmd file and run it with cmd.exe.

Sign up to request clarification or add additional context in comments.

Comments

1

Most developers would use a library to make a MySQL query from Java instead of trying to write commands to an external program and collect the output.

See, for instance, http://dev.mysql.com/doc/refman/5.6/en/connector-j-usagenotes-statements.html#connector-j-examples-execute-select

2 Comments

I also use jdbc for excecuting the sql statements but in my case the communication is between different servers and the fastest way to do that is through the shell command i used.
Ah, ok. I wasn't sure what you were trying to do, really.

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.