vignesh kalai
vignesh kalai

Reputation: 23

Executing awk command in java

i am trying to execute awk command in java for linux/unix os but the thing is when i execute the command it does not show any error it.But after execution there is no output and it takes fraction of second to execute i dont know the problem please help .

the code is

process p =new process():
yes = "awk '{print $1}' /root/Desktop/net/net.zone >> /root/Desktop/net/net.txt";
p = Runtime.getRuntime().exec(yes);

Thank you for your help

Upvotes: 2

Views: 3532

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328774

Starting command line processes correctly with Java isn't easy. I suggest you use commons-exec instead of trying it yourself.

Now you have two things in the command line which need special handing:

  • Single quotes around the AWK script. When you pass each argument as a individual strings to CommandLine via addArgument, you don't need the quotes anymore.

  • The output redirection.

    Since you create a child process, you are in control of stdin and stout. That means you need to open the target file for append in Java, wrap it in a PumpStreamHandler and pass that to DefaultExecutor. See this question for details: Process output from apache-commons exec

Upvotes: 4

Related Questions