Shell redirect from java help needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iksrazal@terra.com.br

    #1

    Shell redirect from java help needed

    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=mypass -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","mypas s","/home/crissilva/c4.sql");
    }

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

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

    StreamGobbler(I nputStream 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) ;

    InputStreamRead er isr = new InputStreamRead er(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.prin tln(type + ">" + line);
    }
    if (pw != null)
    pw.flush();
    } catch (IOException ioe)
    {
    ioe.printStackT race();
    }
    }
    }

    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.prin tln(cmd[0] + " " + cmd[1] + " " +
    cmd[2] + " " + cmd[3] + " " +
    cmd[4] + " " + cmd[5]);

    try {

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

    // any error message?
    StreamGobbler errorGobbler = new
    StreamGobbler(p roc.getErrorStr eam(), "ERROR",
    System.err);

    // any output?
    StreamGobbler outputGobbler = new
    StreamGobbler(p roc.getInputStr eam(), "OUTPUT",
    System.out);

    // kick them off
    errorGobbler.st art();
    outputGobbler.s tart();

    int exitVal = proc.waitFor();
    System.out.prin tln("ExitValue: " + exitVal);
    } catch (Exception e) {
    e.printStackTra ce();
    }

    } // end loadData()
    }

  • Bill Karwin

    #2
    Re: Shell redirect from java help needed

    iksrazal@terra. com.br wrote:[color=blue]
    > String[] cmd = new String[]{"/usr/bin/mysql",
    > dbname,
    > "--user=" + dbuser,
    > "--password=" + dbpassword,
    > "-e",
    > "\"source " + scriptpath + "\""
    > };[/color]

    Take the \" quotes out of the last String in that array.

    Regards,
    Bill K.

    Comment

    Working...