How to execute commands in JAVA....

22 April, 2009 | | 1 comments |

Share |
Java as we all know is platform-independent but there are times when we have to use some external services/commands/applications or some provided by the OS. And infact rather then calling it SOMETIMES it's very much a mundane thing in projects. So I'll teach the entire thing here.....

For executing commands, we use a class known as "Process" which is an abstract class defined in java.lang package. The command along with it's arguments should be defined in a String array. This String array should be supplied to the Process using it's requisite 'exec()' method to execute the command. Also the output(if any) generated on the console(or command window) can also be retrieved.

Here are some examples:

1] To execute a timed shutdown 120 seconds later in Windows XP:

Note: The utility used here is "shutdown.exe" which is present in the 'system32' folder of 'Windows' and since 'system32' folder is there in the environmental variable list it can be used directly.

try
{
String command1=new String[]{"shutdown.exe","-s","-t","120"};
Process ps1=Runtime.getRuntime().exec(command1);
InputStreamreader ISR=new InputStreamReader(ps1.getInputStream());
BufferedReader BR=new BufferedReader(ISR);

String s5=BR.readLine();

System.out.println(s5); //The OP of the command is printed on the console
}

catch(Exception e4)
{
e4.printStackTrace();
}

2] To display an HTML page in "Internet Explorer" in Windows XP:

Note: The utility used here is "iexplore" which is present in the 'Program Files/Internet Explorer' folder of %HOMEDRIVE% and since 'Program Files/Internet Explorer' folder is not there in the environmental variable list it cannot be used directly. Here the absolute path of the utility has to be provided.

try
{
String command1=new String[]{"%HOMEDRIVE%\\Program Files\\Internet Explorer\\iexplore.exe","abc.html"};
Process ps1=Runtime.getRuntime().exec(command1);
InputStreamreader ISR=new InputStreamReader(ps1.getInputStream());
BufferedReader BR=new BufferedReader(ISR);

String s5=BR.readLine();

System.out.println(s5); //The OP of the command is printed on the console
}

catch(Exception e4)
{
e4.printStackTrace();
}

3] To get the environmental variable %username% (which gives the current logged user) in Windows XP:

Note: The utility used here is "echo" which is present in the 'system32' folder of WINDOWS and since 'system32' folder is there in the environmental variable list it can be used directly. But 'echo' is an internal command in Windows and hence can only be used with the 'cmd /k' switch in JAVA. Other internal commands are ''del','ren','attrib' etc.

try
{
String command1=new String[]{"cmd","/k","echo","%username%"};
Process ps1=Runtime.getRuntime().exec(command1);
InputStreamreader ISR=new InputStreamReader(ps1.getInputStream());
BufferedReader BR=new BufferedReader(ISR);

String s5=BR.readLine();

System.out.println(s5); //The OP of the command is printed on the console
}

catch(Exception e4)
{
e4.printStackTrace();
}

NOTE: The other methods which can be used with Process are 'destroy()' (which can be used to terminate a runing process), 'waitFor()' (which causes the current thread to wait, if necessary, until the process represented by this Process object has terminated).

Usage:
p1.destroy(); //Where p1 is object of Process

1 comments:

Anonymous said...

Hello
alprazolam xanax
General supportive measures must be employed together with immediate gastric lavage.
[url=http://www.sistersandroses.com/]xanax no rx[/url]
Levels of alcohol seen in some of these patients have been lower than those usually associated with alcohol-induced fatality.
http://www.sistersandroses.com/ - xanax online
Fatalities have also been reported in patients who have overdosed with a combination of a single benzodiazepine including alprazolam and alcohol.