File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Interacting with Windows command prompt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interacting with Windows command prompt" Watch "Interacting with Windows command prompt" New topic

Interacting with Windows command prompt

Philip Jenson

Joined: Jan 24, 2007
Posts: 21
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

Joined: Feb 24, 2006
Posts: 6662

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

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15094

And if you use Java 5 or newer, you can use class ProcessBuilder. Look it up in the API documentation.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Interacting with Windows command prompt
It's not a secret anymore!