I'm trying to call shell scripts from Java for DB operations. Using a very simple test, for some reason, one line of (ALTER TABLE) in the shell simply won't be executed while the exit value from the call is "0" (valid exit). When I run the script directly in Unix, it gets executed perfectly fine!! So it is not script error but the workings of this Runtime or something else. Please help!!:wall:
My Java program is:
public class scriptBuilder {
private static final Logger logger = LoggerFactory.getLogger(scriptBuilder.class);
public scriptBuilder() {
}
public void writeScript(String scriptFileName) throws java.io.IOException {
Runtime rt = Runtime.getRuntime();
String script = scriptFileName;
rt.exec("chmod 777 " + script);
Process p = null;
try {
String shellCmd = "./" + script;
p = rt.exec(shellCmd);
int exitVal = p.waitFor();
logger.info("rt.exec exit value = " + exitVal);
} catch (Exception e1) {
e1.printStackTrace();
}
String shellfilename = "testing.txt";
if (new File(shellfilename).exists()) {
logger.info(shellfilename+" File exists");
}
}
}
public static void main(String []args) {
try {
sb.writeScript("testingScriptBuilder.sh");
} catch (IOException e) {
e.printStackTrace();
}
}
The shell script being called "testingScriptBuilder.sh" is as below:
#!/bin/bash
DBCON="mysql -usmartie -phal77berd"
DPS_DB="dps4"
function d {
$DBCON << EOF1
ALTER TABLE ${DPS_DB}.sz_avm_g1 ADD COLUMN test_col INT(1);
DROP TABLE IF EXISTS ${DPS_DB}.test_script;
CREATE TABLE ${DPS_DB}.test_script (property_id int(12));
EOF1
}
d
No matter how I organize this script - using function or not, rearraging the order of the lines - the later table line:
ALTER TABLE ${DPS_DB}.sz_avm_g1 ADD COLUMN test_col INT(1);
simply won't be executed!!!!!
When run standalone in the Unix, this line gets executed perfectly fine!!!!!!
PLEASE HELP IF YOU CAN.... THANKS A LOT.:wall: