2

I am interested in executing the cmd.exe move command from a Java application.

I know how to invoke external processes from within Java. There are 100's of questions on this point in StackOverflow. What I can't figure out, is what the command line should look like.

For example, in a DOS window, this is the command I type:

move dirA dirB

and the directory dirA is moved to directory dirB. Exactly what I want done. For reasons that require far too much context, and will detract from the question, a pure java solution is not an option in the specific context I am concerned with. (Oh the joys of operating in an enormous and complex legacy systems!)

What is the external system command I invoke from java to do that? It seems like it should just be:

"cmd move dirA dirB"

However, that fails for me. I am familiar with this question, but it does not directly answer my question.

Community
  • 1
  • 1
John
  • 5,735
  • 3
  • 46
  • 62

1 Answers1

1

No guarantees but I suppose, that you need to prepend the command to run the Windows command shell cmd /c to the command you want to execute. The /c switch terminates the command shell after the desired command completes.
Try:

Runtime.getRuntime().exec("cmd /c move dirA dirB");

EDIT:

As Ian Roberts have noticed in his comment below, it is also important to take into consideration situation when one or both of directories have spaces in their paths. It is thus much safer to use separate strings to form the finall command, i.e like below:

new ProcessBuilder("cmd", "/c", "move", pathToDirA, pathToDirB).start()
Community
  • 1
  • 1
Zegar
  • 1,005
  • 10
  • 18
  • 3
    Note that the single-string version of `exec` is rather simplistic in how it splits up the command line into separate tokens. If there's any chance that the names of dirA and/or dirB might contain spaces then you should use the `String[]` version and split it up yourself. Or better still, `new ProcessBuilder("cmd", "/c", "move", pathToDirA, pathToDirB).start()` – Ian Roberts Nov 16 '13 at 00:17
  • It's of course true, the question hoowever is about the syntax of external command which should be used. I've tried to provide answer in it's basic form. Your contribution considers rather how to make it's less error prone. – Zegar Nov 16 '13 at 01:33
  • The `/c` was the ticket. – John Nov 18 '13 at 19:16
  • @IanRoberts: does `ProcessBuilder` actually attempt to quote the individual parameters if they have spaces in them? (Remember that in Windows, the single-string version is the native one; that is, if you pass an array of tokens they have to be concatenated together into a single string to be passed to the new process.) – Harry Johnston Nov 25 '13 at 08:20
  • 1
    @HarryJohnston apparently so, see around line 130 of [this file](http://hg.openjdk.java.net/jdk7/modules/jdk/file/134fd1a656ea/src/windows/classes/java/lang/ProcessImpl.java). In fact, if you use the single-string `Runtime.exec` this will split up the string into separate arguments and pass these to the `String[]` version, which (on Windows) will then re-combine them with appropriate quoting into a single string to pass to the native side... – Ian Roberts Nov 25 '13 at 10:54
  • @IanRoberts: ouch, that's something to be aware of. Most of the time it won't matter, but if (for example) the command syntax requires extra whitespace or uses a different quoting mechanism then `ProcessBuilder` is going to mangle it. – Harry Johnston Nov 25 '13 at 23:23