RunTimeExec redirection help needed (mysql)

Discussion in 'Java' started by iksrazal@terra.com.br, Jul 6, 2005.

  1. Guest

    Hi all,

    I've been struggling to make this command work from Java:

    /usr/bin/mysql c4 --user=root --password=mypass -e "source
    /home/crissilva/c4.sql"

    Works fine as shown when run from the shell. Using my Java program I
    get:

    [java] /usr/bin/mysql c4 --user=root --password=mother -e "source
    /home/crissilva/c4.sql"
    [java] ERROR>ERROR 1064 (42000) at line 1: You have an error in
    your SQL syntax; check the manual that corresponds to your MySQL server
    version for the right syntax to use near '"source
    /home/crissilva/c4.sql"' at line 1
    [java] ERROR 1064 (42000) at line 1: You have an error in your SQL
    syntax; check the manual that corresponds to your MySQL server version
    for the right syntax to use near '"source /home/crissilva/c4.sql"' at
    line 1
    [java] ExitValue: 1

    This should return zero. non-zero is OS dependant, and I'm unclear if
    mysql effects the error code in any way.

    This is my code, any help highly appreciated. Note I use the
    StreamGobbler class from the java world article.

    package com.whitezone;

    import javax.sql.*;
    import java.sql.*;
    import java.io.*;
    import java.util.*;

    public class RunScript
    {
    public static void main(String[] args) throws Exception
    {
    RunScript rs = new RunScript();
    rs.loadData("c4","root","mypass","/home/crissilva/c4.sql");
    }

    class StreamGobbler extends Thread
    {
    InputStream is;
    String type;
    OutputStream os;

    StreamGobbler(InputStream is, String type)
    {
    this(is, type, null);
    }

    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
    this.is = is;
    this.type = type;
    this.os = redirect;
    }

    public void run()
    {
    try
    {
    PrintWriter pw = null;
    if (os != null)
    pw = new PrintWriter(os);

    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line=null;
    while ( (line = br.readLine()) != null)
    {
    if (pw != null)
    pw.println(line);
    // this does no redirection - just formatting to screen
    System.out.println(type + ">" + line);
    }
    if (pw != null)
    pw.flush();
    } catch (IOException ioe)
    {
    ioe.printStackTrace();
    }
    }
    }

    public void loadData(String dbname, String dbuser, String dbpassword,
    String scriptpath) {

    String[] cmd = new String[]{"/usr/bin/mysql",
    dbname,
    "--user=" + dbuser,
    "--password=" + dbpassword,
    "-e",
    "\"source " + scriptpath + "\""

    };
    System.err.println(cmd[0] + " " + cmd[1] + " " +
    cmd[2] + " " + cmd[3] + " " +
    cmd[4] + " " + cmd[5]);

    try {

    Runtime rt = Runtime.getRuntime();
    //Process proc = rt.exec(wtf);
    Process proc = rt.exec(cmd);

    // any error message?
    StreamGobbler errorGobbler = new
    StreamGobbler(proc.getErrorStream(), "ERROR",
    System.err);

    // any output?
    StreamGobbler outputGobbler = new
    StreamGobbler(proc.getInputStream(), "OUTPUT",
    System.out);

    // kick them off
    errorGobbler.start();
    outputGobbler.start();

    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    } catch (Exception e) {
    e.printStackTrace();
    }

    } // end loadData()
    }
    , Jul 6, 2005
    #1
    1. Advertising

  2. On 5 Jul 2005 19:21:23 -0700, wrote:
    > I've been struggling to make this command work from Java:
    >
    > /usr/bin/mysql c4 --user=root --password=mypass -e "source
    > /home/crissilva/c4.sql"
    >
    > String[] cmd = new String[]{"/usr/bin/mysql",
    > dbname,
    > "--user=" + dbuser,
    > "--password=" + dbpassword,
    > "-e",
    > "\"source " + scriptpath + "\""
    >
    > };


    When you use exec(String[]) you *must* not add extra quotation marks
    to group the arguments the way you do from the command line. The
    grouping is already implied by the elements of the array and the
    arguments are passed exactly "as is" to the program, including any
    extra quotation marks that normally would have been removed by a
    command shell.

    The final array element should be: "source" + scriptpath

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jul 6, 2005
    #2
    1. Advertising

  3. Guest

    That worked, thanks! Also you may be interested in a pure NIO solution
    that someone showed me - much simpler as it doesn't need the inner
    class. I'm including both versions for the benefit of others. Note:
    need to create db first.

    iksrazal

    package com.whitezone;

    import javax.sql.*;
    import java.sql.*;
    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.util.*;

    public class RunScript
    {
    public static void main(String[] args) throws Exception
    {
    RunScript rs = new RunScript();
    //rs.loadDataWithNIO("c4","root","mypass","/home/iksrazal/c4.sql");
    rs.loadData("c4","root","mypass","/home/iksrazal/c4.sql");
    }

    class StreamGobbler extends Thread
    {
    InputStream is;
    String type;
    OutputStream os;

    StreamGobbler(InputStream is, String type)
    {
    this(is, type, null);
    }

    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
    this.is = is;
    this.type = type;
    this.os = redirect;
    }

    public void run()
    {
    try
    {
    PrintWriter pw = null;
    if (os != null)
    pw = new PrintWriter(os);

    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line=null;
    while ( (line = br.readLine()) != null)
    {
    if (pw != null)
    pw.println(line);
    // this does no redirection - just formatting to screen
    System.out.println(type + ">" + line);
    }
    if (pw != null)
    pw.flush();
    } catch (IOException ioe)
    {
    ioe.printStackTrace();
    }
    }
    }

    public void loadData(String dbname, String dbuser, String
    dbpassword, String scriptpath) {

    String[] cmd = new String[]{"/usr/bin/mysql",
    dbname,
    "--user=" + dbuser,
    "--password=" + dbpassword,
    "-e",
    "source " + scriptpath

    };

    System.err.println(cmd[0] + " " + cmd[1] + " " +
    cmd[2] + " " + cmd[3] + " " +
    cmd[4] + " " + cmd[5]);

    try {

    Runtime rt = Runtime.getRuntime();
    //Process proc = rt.exec(wtf);
    Process proc = rt.exec(cmd);

    // any error message?
    StreamGobbler errorGobbler = new
    StreamGobbler(proc.getErrorStream(), "ERROR",
    System.err);

    // any output?
    StreamGobbler outputGobbler = new
    StreamGobbler(proc.getInputStream(), "OUTPUT",
    System.out);

    // kick them off
    errorGobbler.start();
    outputGobbler.start();

    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    } catch (Exception e) {
    e.printStackTrace();
    }

    } // end loadData()

    public void loadDataWithNIO(String dbname, String dbuser, String
    dbpassword, String scriptpath) {

    String[] cmd = new String[]{"/usr/bin/mysql",
    dbname,
    "--user=" + dbuser,
    "--password=" + dbpassword
    };
    System.err.println(cmd[0] + " " + cmd[1] + " " +
    cmd[2] + " " + cmd[3]);

    try {

    Runtime rt = Runtime.getRuntime();
    Process fRuntimeProcess = rt.exec(cmd);
    FileInputStream fis = new FileInputStream(scriptpath);
    FileChannel fChan = fis.getChannel();
    fChan.transferTo(0, fChan.size(),
    Channels.newChannel(fRuntimeProcess.getOutputStream()));

    } catch (Exception e) {
    e.printStackTrace();
    }

    } // end loadData()
    }
    , Jul 6, 2005
    #3
  4. Guest

    That worked, thanks! Also you may be interested in a pure NIO solution
    that someone showed me - much simpler as it doesn't need the inner
    class. I'm including both versions for the benefit of others. Note:
    need to create db first.

    iksrazal

    package com.whitezone;

    import javax.sql.*;
    import java.sql.*;
    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.util.*;

    public class RunScript
    {
    public static void main(String[] args) throws Exception
    {
    RunScript rs = new RunScript();
    //rs.loadDataWithNIO("c4","root","mother","/home/iksrazal/c4.sql");
    rs.loadData("c4","root","mother","/home/iksrazal/c4.sql");
    }

    class StreamGobbler extends Thread
    {
    InputStream is;
    String type;
    OutputStream os;

    StreamGobbler(InputStream is, String type)
    {
    this(is, type, null);
    }

    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
    this.is = is;
    this.type = type;
    this.os = redirect;
    }

    public void run()
    {
    try
    {
    PrintWriter pw = null;
    if (os != null)
    pw = new PrintWriter(os);

    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line=null;
    while ( (line = br.readLine()) != null)
    {
    if (pw != null)
    pw.println(line);
    // this does no redirection - just formatting to screen
    System.out.println(type + ">" + line);
    }
    if (pw != null)
    pw.flush();
    } catch (IOException ioe)
    {
    ioe.printStackTrace();
    }
    }
    }

    public void loadData(String dbname, String dbuser, String
    dbpassword, String scriptpath) {

    String[] cmd = new String[]{"/usr/bin/mysql",
    dbname,
    "--user=" + dbuser,
    "--password=" + dbpassword,
    "-e",
    "source " + scriptpath

    };

    System.err.println(cmd[0] + " " + cmd[1] + " " +
    cmd[2] + " " + cmd[3] + " " +
    cmd[4] + " " + cmd[5]);

    try {

    Runtime rt = Runtime.getRuntime();
    //Process proc = rt.exec(wtf);
    Process proc = rt.exec(cmd);

    // any error message?
    StreamGobbler errorGobbler = new
    StreamGobbler(proc.getErrorStream(), "ERROR",
    System.err);

    // any output?
    StreamGobbler outputGobbler = new
    StreamGobbler(proc.getInputStream(), "OUTPUT",
    System.out);

    // kick them off
    errorGobbler.start();
    outputGobbler.start();

    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    } catch (Exception e) {
    e.printStackTrace();
    }

    } // end loadData()

    public void loadDataWithNIO(String dbname, String dbuser, String
    dbpassword, String scriptpath) {

    String[] cmd = new String[]{"/usr/bin/mysql",
    dbname,
    "--user=" + dbuser,
    "--password=" + dbpassword
    };
    System.err.println(cmd[0] + " " + cmd[1] + " " +
    cmd[2] + " " + cmd[3]);

    try {

    Runtime rt = Runtime.getRuntime();
    Process fRuntimeProcess = rt.exec(cmd);
    FileInputStream fis = new FileInputStream(scriptpath);
    FileChannel fChan = fis.getChannel();
    fChan.transferTo(0, fChan.size(),
    Channels.newChannel(fRuntimeProcess.getOutputStream()));

    } catch (Exception e) {
    e.printStackTrace();
    }

    } // end loadData()
    }
    , Jul 6, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Hofer
    Replies:
    1
    Views:
    392
  2. listaction

    mysql-ruby help needed please

    listaction, Feb 1, 2005, in forum: Ruby
    Replies:
    2
    Views:
    129
    listaction
    Feb 2, 2005
  3. Brian Ablaza
    Replies:
    1
    Views:
    118
    zakifataya
    Feb 27, 2006
  4. Ryan Macy

    Ideas needed & help needed!

    Ryan Macy, Jul 19, 2006, in forum: Ruby
    Replies:
    2
    Views:
    492
    Ryan Macy
    Jul 19, 2006
  5. Wrapster Aol

    ruby /mysql needed help asap..

    Wrapster Aol, Jul 23, 2009, in forum: Ruby
    Replies:
    3
    Views:
    96
Loading...

Share This Page