Submit Your Article
Home Articles News Tutorials Videos Add An Article
Topics: Design Photoshop Programming PHP CSS Java Database Web Development Javascript Ajax
– Close + Open

Find Out More About DevWebPro!

Sign up for the newsletter


» Terms & Conditions

Welcome to the New DevWebPro!

DevWebPro Includes:
  Hundreds Of Tutorials   Developer News
  Unique Gadget Videos   Tons of Topics to Discuss
  Expert Advice   We Will Publish Your Articles

Starting System Processes Through JAVA Exec Command

By: Vaibhav Pandey
Tuesday, October 27th, 2009
Text: Decrease Font Size Increase Font Size | Print Print Article | Share: Delicious Digg StumbleUpon Post to Twitter Post to Facebook

Java is capable of executing threads by starting and running them. Java Programming language also provides you with the ability to start the heavyweight system processes. Threads are lightweight processes and can be called as a subprocess. Executing or starting a System process is completely different than executing a thread.

Java. lang provides a method exec() which is used to name and run the process. The exec() method returns a process object and is invoked on Runtime instance. Runtime is an abstract class whcih can be instantiated as the following:

Runtime rt = Runtime. getRuntime();

While you write and use exec() in your programs do remember that exce() method throws an IOException which needs to be handled as it is a checked Exception. Make sure to write the exec() inside a try/catch block which can handle an IOException.

The process object which is used to get the value from exec() can be used to control how your Java program interacts with the running process.
The exec() method is an environment dependent method so do make sure to write OS specific process names as arguments to exec(). The argumemnts passed must be system recognizable commands.

Here the following example shows how the Window’s notepad is launched through the Java program. Notepad is a simple text editor of windows. The example uses exec() method to invoke the notepad process.

class LaunchNotepad{
public static void main(String a[]){
Runtime rt = Runtime. getRuntime();
Process p =null;
try{
p=rt. exec(“notepad”);
}catch(Exception e)
{System. out. println(“Error in Starting Notepad”);}
}
}

Now you system is up and running the notepad process. Similarly you can invoke any process you like through you Java Application what you have to know is the system call which will be passed as argument in the exec() method.

Comments

Topics: ,

About the Author:
Vaibhav Pandey got offered employment from an Indian Multinational IT Company. He is 21 years old. He has a huge interest in Java programming and has liked it from his study days. Vaibhav loves to blog and share his experiences and thoughts. He now resides in Lucknow, a state capital in India. Check out his blog at http://javatutorialsworld.blogspot.com.
DevWebPro is an iEntry Network ® publication - © 1998-2010 All Rights Reserved