Why only partiial shell script got executed when called in Java

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:

777 is not the magic sledgehammer to fix all permissions problems. By using it, you've made that script writable by any user on that system. Literally anyone could edit it and maybe put 'DROP DATABASES' into it. This is bad.

What happens when you execute this script from a proper shell? It might be printing error messages you're not seeing.

Also, use code tags for code please. Whitespace is important in some places in shell scripts and not putting your code in code tags does not preserve this.

  • Thanks for the suggestion on 777.
  • The same script gets executed complete and fine when run from a shell (just not from java)
  • What did you mean by "using code tags"? Sorry I'm new to shell scripting...

He meant this: Video tutorial on how to use code tags

Thanks for the video for cod tags.
I just found that the ALTER TABLE line works on some other table in the same DB but not that table... Still trying to understand this part....
(still, if run the original script from shell, it get executed fine on this table...)