RunTimeExec redirection help needed (mysql)

I

iksrazal

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()
}
 
G

Gordon Beaton

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
 
I

iksrazal

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()
}
 
I

iksrazal

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()
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top