2

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

vignesh kalai
  • 23
  • 1
  • 4

1 Answers1

4

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

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Sir i am new to java cant understand what you are saying can u please elaborate – vignesh kalai Oct 06 '14 at 07:56
  • Please have a look at the documentation which I linked and work through the examples. If you don't know how to add a library to your project, your IDE's documentation should explain that. – Aaron Digulla Oct 06 '14 at 10:21
  • @vigneshkalai since you are new to Java I suggest you ask in a Java forum how to do whatever it is you're trying to do. There's an excellent chance that calling awk is NOT the right approach. – Ed Morton Oct 06 '14 at 16:17
  • thanks bro in stead using unix command i created a shell script and ran it – vignesh kalai Oct 08 '14 at 09:38