• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interacting with Windows command prompt

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic