How to execute multiple command in single line in/using Java?
Interacting with shell is essential in certain situation. Say if you want to scan file(s) using antivirus you have to use shell command in java program.
command[2] = "f: && dir && cd snap"; will execute three command.
Use && in string to use multiple command.
public class cmdtest { public static void main(String[] args) { try { String[] command = new String[3]; command[0] = "cmd"; command[1] = "/c"; command[2] = "f: && dir && cd snap"; Process p = Runtime.getRuntime().exec(command); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = reader.readLine(); while (line != null) { System.out.println(line); line = reader.readLine(); } BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); String Error; while ((Error = stdError.readLine()) != null) { System.out.println(Error); } while ((Error = stdInput.readLine()) != null) { System.out.println(Error); } } catch (Exception e) { e.printStackTrace(); } } }
command[2] = "f: && dir && cd snap"; will execute three command.
- - Move cursor in F:\ drive
- - List all directory of drive
- - Move cursor in snap directory
Use && in string to use multiple command.
Update
Thanks to @Suri(http://www.javaquery.com/2011/03/how-to-execute-multiple-command-in.html#comment-1524448304) for pointing out the command line execution part that I didn't cover. There are few commands requires user input. Following code demonstrate user input.
public class cmdtest { public static void main(String[] args) { try { /* String array to execute commands */ String[] command = new String[3]; command[0] = "cmd"; command[1] = "/c"; /* Command you want to execute */ command[2] = "c: && date && date"; /* Create process */ Process p = Runtime.getRuntime().exec(command); /* Get OuputStream */ PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream())), true); /* Read the output of command prompt */ BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = reader.readLine(); /* Read upto end of execution */ while (line != null) { /* Pass the value to command prompt/user input */ writer.println("08-08-2014"); System.out.println(line); line = reader.readLine(); } /* The stream obtains data from the standard output stream of the process represented by this Process object. */ BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); /* The stream obtains data from the error output stream of the process represented by this Process object. */ BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); String Input; while ((Input = stdInput.readLine()) != null) { System.out.println(Input); } String Error; while ((Error = stdError.readLine()) != null) { System.out.println(Error); } } catch (Exception e) { e.printStackTrace(); } } }
You need to change the user input depending on your command at writer.println("08-08-2014");. In this example "date" command required date in (mm-dd-yy) format.
Hi, I have a question for you.
ReplyDeleteHow is it that by passing in an array, command line is able to recognize them as different command? Is it a build in function or anything?
Also, I need to allow administrator access and take in the password which the user entered, as the runas command requires users to enter the password. After this, i would also need to run another command which requires administrator rights. Hot do i put them together?
Thanks in advance
When we use "&&" is separates the command. Here we creating instance of MS shell to execute command.
ReplyDelete1. You want to enter password then use the input stream reader. and pass to the class which contains the logic for admin access.
2. When it executes use second class which have other logic for command lines.
while ((Error = stdInput.readLine()) != null) {
//Error will give you the output of command line
}