[Java] Run System Commands

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

[Java] Run System Commands

Post by terminus on Wed Apr 23, 2008 1:16 pm
([msg=1097]see [Java] Run System Commands[/msg])

Often times a system administrator needs to develop a script or a piece of code to run system commands. For example: automated cleanup, encryption/decryption, monitoring, or other processes may be a regular part of a system administrators day. One way to accomplish these tasks in an inexpensive fashion is to use open source software. I have found myself writing BASH shell scripts to automate calling these applications to perform their duties. However, BASH is limited. What if I want to create graphical reports and statistics of the activities of my open source applications and their results? What if I need a GUI to manage all of the open source applications that I have installed? One way to go about this is using Java.

Java can be useful to run system commands or to run a piece of open source software that has been installed. In order to connect the beautiful GUI application that you have spent many hours of drag and drop designing you will need to know how to call these programs and system commands. I will present one way of approaching this challenge:

Every Java application is able to interact with it's environment. Associated with a Java class is an instance of a class called java.lang.Runtime(1). In order to obtain the current Runtime instance the static method java.lang.Runtime.getRuntime() may be called. The getRuntime method will return a Runtime object. You might be wondering how this is useful to you. You do not need to keep a Runtime object around for future use (in fact a class can not create its own instance of this class as specified in the API). Simply create a java.lang.Process(2) object and assign the Runtime object to it. Example:

String command = "some command to run";
Process myProcess = null;

try {
myProcess = Runtime.getRuntime().execute(command);
} catch (IOException e) {
//however you want to handle this

Please note that execute(command) throws an IOException that must be caught or you will receive a compile time error.

Now your class may execute a system command or application simply by specifying the command to execute and the arguments to the command. Simple as pie! In addition, output from the application may be obtained using the getInputStream() method of your Process object. Error data may be obtained using the getErrorStream() method.

http://java.sun.com/javase/6/docs/api/j ... ntime.html (1)
http://java.sun.com/javase/6/docs/api/j ... ocess.html (2)
New User
New User
Posts: 45
Joined: Mon Apr 14, 2008 9:10 am
Blog: View Blog (0)

Re: [Java] Run System Commands

Post by TheMindRapist on Mon May 26, 2008 8:59 pm
([msg=3293]see Re: [Java] Run System Commands[/msg])

This is nice, I'll be trying it at school.
User avatar
Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests