Computing.Net > Forums > Unix > run unix command thr java program

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

run unix command thr java program

Reply to Message Icon

Name: rasica
Date: January 5, 2004 at 10:30:04 Pacific
OS: SCO-Unix
CPU/Ram: 128
Comment:

hi all,
Can we call(run) unix commands through a java program? Plzz let me know....

Actually i want the commands like "ls" to run automatically when some event occurs & then i want to store its output in a file by redirecting it.

if anybody knows any link or the solution...plzz tell me..
Thanx in advance....waiting 4 ur reply....



Sponsored Link
Ads by Google

Response Number 1
Name: wharfie
Date: February 26, 2004 at 15:01:35 Pacific
Reply:

use the java.lang.Runtime's exec() method.

this is documented at;
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html

As for redirecting output to a file i am having problems running commands with output redirection under Redhat Linux 9. The > symbol is not being interpreted correctly. For example ls > fred.txt results in an error message >: No such file or directory. A work around is to use a shell script or don't use redirection and read the commands stdout via your Java. It depends on what you ultimately want to do with the commands output.

Below is a code example. Simply change the values of cmd and workDir to suit your situation.

import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* @author wharfie
* 2004-02-27
* Run a system command
*/
public class RunSystemCommand {
public static void main(String args[]) {
String s = null;
// system command to run
String cmd = "ls > fred.txt";
// set the working directory for the OS command processor
File workDir = new File("/dir1/dir2");

try {
Process p = Runtime.getRuntime().exec(cmd, null, workDir);
int i = p.waitFor();
if (i == 0){
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
}
else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdErr.readLine()) != null) {
System.out.println(s);
}

}
}
catch (Exception e) {
System.out.println(e);
}
}
}


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: run unix command thr java program

Running Unix commands in parallel www.computing.net/answers/unix/running-unix-commands-in-parallel/6259.html

running unix command in vi session www.computing.net/answers/unix/running-unix-command-in-vi-session/7869.html

Run Unix command in DOS FTP prompt. www.computing.net/answers/unix/run-unix-command-in-dos-ftp-prompt/8494.html