0

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 =)

Kamron K.
  • 594
  • 1
  • 10
  • 18
  • First, you should log the IOExceptions in the catch block (for testing e.printStackTrace() should be enough). Second, does the problem occur as well if you just execute `mv` (exec3)? – home Jun 24 '12 at 05:34
  • Nope, i can copy and paste the command printed from the constructor, and it works just fine. – Kamron K. Jun 24 '12 at 05:42
  • And no exception is getting thrown in the main program i have this sample implemented ?? – Kamron K. Jun 24 '12 at 05:43
  • Regarding the exception: given your sample program you cannot even know if an IOException is thrown - because of the empty catch block. – home Jun 24 '12 at 05:44
  • I agree, but like i say, this is just a sample – Kamron K. Jun 24 '12 at 05:50
  • In my actual code i am issuing a dynamic number of commands, sometimes in the hundreds depending on the day and situation, so i am asking to see if anyone has run into this problem before, or heard of this problem before – Kamron K. Jun 24 '12 at 05:52
  • In my actual code, I have the exceptions printing to the console for now as it hasn't been released from monitored testing stages – Kamron K. Jun 24 '12 at 05:52

1 Answers1

0

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Noted, thank you for the advice. I was not aware of the waitFor() method in the Process library. I will try that, and if the problem persists, at that point i will resort to posting my actual code. Thank you for answering my question the best you could with my lack of info, i really appreciate it =) – Kamron K. Jun 24 '12 at 20:36
  • The waitFor() method is what did it, now everything is running smoothly and exactly as expected. – Kamron K. Jun 25 '12 at 14:13