Connecting Tech Pros Worldwide Forums | Help | Site Map

Shell redirect from java help needed

iksrazal@terra.com.br
Guest
 
Posts: n/a
#1: Jul 23 '05
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","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()
}

Bill Karwin
Guest
 
Posts: n/a
#2: Jul 23 '05

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.
Closed Thread