File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interacting with Windows command prompt

 
Philip Jenson
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I constantly have to access the command prompt to execute a particular command. The command is always the same except for one parameter, which is in the middle of the command. I started writing a batch file that would prompt me for the one parameter and execute the command.

However, I thought it would be pretty easy to write a Java program that would prompt for the the parameter and execute the command in the Windows command prompt. I thought that maybe the Java program would have to call the batch file, but I don't think this would be preferable. For one, I'm not sure if the Java program can accept an input and then pass said input to a batch file.

Would this be a good idea for a quick Java program or would it be a better idea to write a program like this in a different language? I'm taking a Java class now, but I have no idea how to execute commands in the Windows command prompt from Java.

Let me know what you think. Thanks!
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use exec() in the Runtime class to execute other processes. The process path has to exist in the path variable or you have to mention the fully qualified process name, like c:\usr\bin\example.exe
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you use Java 5 or newer, you can use class ProcessBuilder. Look it up in the API documentation.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic