1

May be turned out to be a silly problem. Here is the code:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Test {
    public static void main(String[] args) throws Exception{
        String command = "mysql -hlocalhost -uroot -padmin < ./scripts/atestscript.sql";
//      command = "cat < ./scripts/atestscript.sql";
        Process p = Runtime.getRuntime().exec(command);


        InputStream inputstream = p.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

        // read the output
        String line;
        while ((line = bufferedreader.readLine()) != null) {
            System.out.println(line);
        }

        p.waitFor();
    }
}

When I run this program to execute the sql script on MySQL, I just only got the output showing the usage information about mysql command, as following: mysql Ver 14.14 Distrib 5.5.44, for debian-linux-gnu (x86_64) using readline 6.3 Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]
  -?, --help          Display this help and exit.
  -I, --help          Synonym for -?
  --auto-rehash       Enable automatic rehashing. One doesn't need to use
                      'rehash' to get table and field completion, but startup
                      and reconnecting may take a longer time. Disable with
                      --disable-auto-rehash.
                      (Defaults to on; use --skip-auto-rehash to disable.)

The command mysql -h localhost -u root -p admin < ./scripts/atestscript.sql runs perfectly when execute directly through the command line. What is wrong with the program when executing the command by folking a shell process?

I then tried the command mysql -h localhost -u root -p admin -e "source ./scripts/atestscript.sql" in the program, it doesn't work neither.

I also tried using ProcessBuilder by replacing Runtime.getRuntime.exec(command) by the following code:

        List<String> params = new ArrayList<String>();
        params.add("/usr/bin/mysql");
        params.add("-hlocalhost");
        params.add("-u" + username);
        params.add("-p" + password);
//      params.add("-e");
//      params.add("\"SOURCE " + filePath + "\"");
        params.add("<");
        params.add(filePath);

        ProcessBuilder pb = new ProcessBuilder(params);
        Process p = pb.redirectErrorStream(true).start();
        int state = p.waitFor();

doesn't work too.

Maybe I just made some tiny mistakes in the program, but I was really stucked on it. Anyone has an idea on the problem?

5
  • no space between -hlocalhost, "-u" + username, "-p" + password could be the issue. Do a display of params before you are calling ProcessBuilder.. Also, hope you know complexity of what you trying to do.. Commented Sep 23, 2015 at 17:14
  • 1
    Java won't automatically do redirection. the "<" is not a parameter, it needs to be interpreted by the shell Commented Sep 23, 2015 at 17:20
  • Duplicate of stackoverflow.com/questions/1955268/… ? Commented Sep 24, 2015 at 5:48
  • @redflar3 Yeah I did in my real code. The space is not an issue if the command is written in a string as a whole. But if the params is seperated feed into a ProcessBuilder like the way I did in the last block of code in my question, then the space is really an issue. no space should be placed right after -p in params.add("-p" + password). Or the access will fail. For the complexity of what I am trying to do, I admit that it's really annoying to dump a database in this way. But I have to do it programmatically as the project requires. Commented Sep 24, 2015 at 8:42
  • @JessBalint Yeah, redirect command should not be used as parameter seperately in this way. Thx. Commented Sep 24, 2015 at 8:53

1 Answer 1

2

I would use ProcessBuilder with params sh, -c and mysql -hlocalhost -uroot -padmin < ./scripts/atestscript.sql. Do not forget about quotes on 3rd param.

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

3 Comments

It works! Thanks a lot for this alternative. In fact I put params.add("mysql -hsomehost -uroot -padmin < ./scripts/xxx.sql"); . Wrap the command in quotes with using params.add() doesn't work.
OK, I'll fix my answer. BTW, passing user and password isn't too safe. I would prefer using appropriate mysql config file.
yeah, thx. In fact the code I gaven is just for demonstrating the problem which was simplified. :)

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.