473,388 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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","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 23 '05 #1
1 2322
ik******@terra.com.br wrote:
String[] cmd = new String[]{"/usr/bin/mysql",
dbname,
"--user=" + dbuser,
"--password=" + dbpassword,
"-e",
"\"source " + scriptpath + "\""
};


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

Regards,
Bill K.
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Bowling | last post by:
I'm creating a routine (not in a browser) to move multiple files on a daily basis to a backup directory. It can be done easily by call shell functions like 'mv file* newdir'. I can't find any...
3
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but...
5
by: Hal Vaughan | last post by:
I have a Java program, composed of a number of classes, that I've been running as a command line program. It logs everything it does and, as it logs each event, it also prints that line of info to...
0
by: Kyle | last post by:
To any who chose to provide an answer, or even any suggestions to this problem, I thank you greatly in advance. +200 pts. for any valid solutions. I am currently in the process of converting a...
6
by: Peter Row | last post by:
Hi, I am writing a DLL in VB.NET that implements IHttpHandler.ProcessRequest. This code calls a sub and I need to know if that sub did a response redirect or not. Specifically I need to know...
21
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will...
3
by: vaskarbasak | last post by:
Hi All, i want to run shell script from java.shell script run successfully and generate a file. But when i want to check the existence of that file,it shows that the file is not present. ...
2
by: smitanaik | last post by:
i have a java file which i am running through shell script. the syntax that i have used is #! /bin/bash javac Copy.java
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.