This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to execute dos commands using RunTime.getRuntime.exec() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to execute dos commands using RunTime.getRuntime.exec()" Watch "how to execute dos commands using RunTime.getRuntime.exec()" New topic
Author

how to execute dos commands using RunTime.getRuntime.exec()

twity Panu
Greenhorn

Joined: Jan 31, 2007
Posts: 7
String[] cmd = new String[4];
cmd[0]= "cmd.exe";
cmd[1] = "/C";
cmd[2] = "D:";
cmd[3] = "dir";
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s = "";
while (( s = stdInput.readLine()) != null) {
System.out.println(s);
}

Actully my default when i type cmd in command center it id pointing to c:\\DocumentsAndSetting\\paul
but i want change it to d:\\
in command prompt if i type d:
it changes,
but not able to do it using getRuntime().exec()

pls provide some help
Nicole Lacoste
Ranch Hand

Joined: Oct 04, 2006
Posts: 30
Hi,

I am not sure that I understand your question... are you asking how to change the directory the process is running in? If so try using a ProcessBuilder and the "ProcessBuilder directory(File directory)" method.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.htm

Niki
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
use ,
java.io.File)]this
version of exec
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

If the only thing you want to do is get a list of files and directories in a specific directory, then you don't need to do this by executing cmd.exe.

Class java.io.File has methods to get a list of files and directories in a directory.

See this for an example: Listing the Files or Subdirectories in a Directory


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41133
    
  45
You might also profitably read this article about the various things to be aware of when using Runtime.exec.


Ping & DNS - my free Android networking tools app
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi twity,

Welcome to JavaRanch!

We have a strict policy on display names, which must be a real first and last name with a space between.

Please go here and fix your display name up, pronto. Thanks, pardner!


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to execute dos commands using RunTime.getRuntime.exec()
 
Similar Threads
How Direct Runtime.getRuntime().exec output to Console window ??
Implementing runAs in Java
Runtime.exec() doubt, please help me...........
how to handle different charset with ProcessBuilder?
How to run simple windows commands through java program?