• Post Reply Bookmark Topic Watch Topic
  • New Topic

Running system command  RSS feed

 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...so i'm wondering, is there a way that I can run a system command from my java program, for example, if I wanted to rename a file in windows, could execute from java a command like "move myfile.exe myfile2.exe"

Regards,

Chris
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, there's a whole API for it:



Specifically, look at the exec() methods. I would go into details with an example, but they can vary, so I'd just search the web for examples of this class. Also, keep in mind the following when you do this:

- The code is less portable since you can only run it on windows computers. You could define multiple calls, one for each OS, but the inherently portability of the java code is lost.

- Sometimes there are issues knowing when command has finished, and sometimes people like to spawn threads for this instead of waiting. Watch out for abnormal behavior such as performing a windows copy that is not ready to be read immediately by your java program.

- If possible (depends on which command you are running) I'd recommend running with no output generated. Sometimes programs have options whether to echo or output information to screen as they run, and I'd recommend disabling this since it will help java understand when the program is complete. (unless you need that output even then I'd pipe it to a file)
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this particular case, you should use the File.renameTo() method. In fact, the File class has a lot of methods that help with file manipulation, so you rarely need to use system commands to do it.

If there are other commands you want to execute, you should look at the Runtime and Process classes. A google search should come up with tutorials about how to use these.

Note that in doing this, you are making your Java program dependent on the underlying platform (operating system). Since Java is supposed to be platform-independent, you should avoid doing this, especially if you want to run your program on different OSes. If you need to run system commands a lot and depending on the purpose for it, you might look into putting them into a system-dependent scrit that is separate from your Java program. In Windows you would use a batch file and in Unix a shell script.

Layne
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

Actually the renaming was just an example...I think truly in my case portability won't suffer cause I would be using for example the "CSound" program, which has the same flags and syntax on many different platforms.

Regards,

Chris
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!