2

I want to execute database import from .sql file from java program. My program is working fine on windows. But I am facing problem on linux machine.

Code -

    try {




ProcessBuilder builder = new ProcessBuilder("mysql -u root -p password db-name < db_script.sql");
            builder.redirectErrorStream(true);
            Process pr = builder.start();

            InputStream is = pr.getInputStream();

            // Now read from it and write it to standard output.
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

I am getting - java.io.IOException: Cannot run program "mysql -u root -p password db-name < db_script.sql": java.io.IOException: error=2, No such file or directory

The above command is working fine on linux terminal. Some one please advice me on this.

Thanks in advance

3 Answers 3

9

The < redirection is a shell thing. Try something like this:

ProcessBuilder builder = new ProcessBuilder("/bin/sh", "-c", "mysql -u root -p password db-name <  b_script.sql");

UPDATE:

Otherwise, if you're using java 7+, you can do the redirection in java:

ProcessBuilder builder = new ProcessBuilder(
        "mysql", "-u", "root", "-p", "password", "db-name");
builder.redirectInput(ProcessBuilder.Redirect.from(new File("b_script.sql")));
Sign up to request clarification or add additional context in comments.

4 Comments

After trying above. I started getting "/bin/sh: Can't open mysql -u root -p password db-name < b_script.sql".
The -c was missing. Sorry about that.
Thanks Maurice for reply. With updated command, it is not giving me error. But at the same database is not getting updated. Surprisingly, I am getting output as we get if type mysql help command on terminal. I don't know what i am missing.
Thanks Maurice for your valuable help. The problem is resolved. I was using space between -p and password. There should be no space between password and -p option. Thanks a million.
0

have you checked that your code is executed from proper dir??

that if your db_script.sql is inhome then you are running from home as

home>mysql -u root -p password db-name < db_script.sql

or provide full path of db_script.sql in the command

1 Comment

I tried by giving full path of .sql file also. But I think the issue does not seems with the file path. Thanks
0

A simple way to achieve it.

String DBUSERNAME = "";
String DBUSERPASSWORD = "";
String sqlfilename = "";
PrintWriter writer = new PrintWriter("tmp.dmp", "UTF-8");
writer.println("mysql --user " + DBUSERNAME + " --password=" + DBUSERPASSWORD + " < " + sqlfilename);
writer.close();

runCommand("chmod 777 tmp.dmp");
runCommand("./tmp.dmp");
runCommand("rm tmp.dmp");

public static void runCommand(String command) throws IOException {
    String s = "";
    Process p = Runtime.getRuntime().exec(command);

    BufferedReader stdInput = null;
    BufferedReader stdError = null;
    try {
        stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        // read the output from the command
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }

        // read any errors from the attempted command
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
    } finally {
        if (stdInput != null) {
            stdInput.close();   
        }

        if (stdError != null) {
            stdError.close();                   
        }
    }
}

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.