Search code examples
javaunixcommandruntimeshellexecute

Not all java commands to unix shell are running


I am writing an application that sends commands to a unix shell.

I am not ever having any troubles with issuing cp and chmod commands (that i know of) but for some reason mv commands will not actually move the files i spedify?

My code can be show cased as follows:

    import java.io.IOException;
    public class ExecuteCommand {   
        public static void main(String[] args){
            ExecuteCommand exec = new ExecuteCommand("cp /some/directory/file.txt /some/directory/of/mine/");
            ExecuteCommand exec2 = new ExecuteCommand("chmod 666 /some/directory/of/mine/file.txt");
            ExecuteCommand exec3 = new ExecuteCommand("mv /some/directory/of/mine/file.txt /some/directory/of/mine/subDirectory/");
        }
        public ExecuteCommand(String command) {
            try {
                    System.out.println("EXECUTING!::" + command);       
                    Process child = Runtime.getRuntime().exec(command);             
            } catch (IOException e) {
            }
        }

    }

I have tried putting timers in between the commands with no progress being made to ensure that %100 of my commands are processed.

Please note that my code includes sample info, if some of the unix file system syntax is incorrect, forgive me, and please do not blame the problem on that.

If you need any further info please ask and i will provide asap

Thanks Guys =)


Solution

  • One problem with your sample code is that you are squashing the exceptions. If there is any problem you are throwing away the evidence.

    A second problem is that you are not waiting for one command to complete before launching the next one. If a later one depends on an earlier one finishing, it will fail.

    A third problem is that you are not checking command return / exit codes, or looking at any error messages that they might produce. The error messages might tell you why commands are not working ... if you bothered to fetch and print them.

    The final (meta-)problem is that this is not your real code ... so we don't know if the problems we are seeing are just artefacts of your Question. Please provide REAL code, not some poorly bowdlerized version.