0

I am new to java and learning it. I want to run a command line program from java class.

This is the command i want to run:(only linux)

path/to/folder/$ echo "Inhibition of NF-kappaB activation reversed the anti-apoptotic effect of isochamaejasmin." | ./geniatagger

This will give me output which I want to store in a java object.

String output:

Inhibition Inhibition NN B-NP O of of IN B-PP O NF-kappaB NF-kappaB NN B-NP B-protein activation activation NN I-NP O reversed reverse VBD B-VP O the the DT B-NP O anti-apoptotic anti-apoptotic JJ I-NP O effect effect NN I-NP O of of IN B-PP O isochamaejasmin isochamaejasmin NN B-NP O . . . O O

Please guide me how to achieve that?

Peeyush Sahu
  • 111
  • 3
  • 9
  • @juan.facorro the solution proposed is suboptimal. To the OP: what is this "some output which I want to store in a java object"? A string output? A binary output? – fge Mar 09 '14 at 20:47
  • Do you really need the `echo` or can the pipe output be done directly in Java? – Axel Mar 09 '14 at 20:51
  • I now see your question in a different way then when I posted my answer. Is ./geniatagger supposed to be your program? That you will be writing? In Java? – Stijn de Witt Mar 09 '14 at 20:58
  • @StijndeWitt yes geniatagger is (unix) command line tool which does Named entity recognition on text and produce the output.. – Peeyush Sahu Mar 09 '14 at 21:33
  • Is it an existing unix tool or are you writing it? If you are writing it in Java then invocation will look a bit different as you don't execute Java programs directly like that. Instead you would be calling `java -jar geniatagger.jar` or something along those lines. Inside the geniatagger program you would be able to read the text that was fed to it by accessing standard in. I updated my answer. – Stijn de Witt Mar 09 '14 at 21:50
  • Sorry I did not make it clear. This is a already written tool [link](http://www.nactem.ac.uk/tsujii/GENIA/tagger/), I am not writing it. I will be feeding a String to it using java program and it will give me an output. This output will be used for the further purposes. I just want to run a command on terminal from java and get the output. If you just goto the webpage of genia tagger (LINK) and scroll down to example, that is what i want to do. – Peeyush Sahu Mar 09 '14 at 23:05

1 Answers1

0

Command line interpreters, mostly called shells, are a typical platform specific feature not supported well by Java. This is not to say it is impossible, but you have to ask yourself whether you want this feature to work on all platforms or not.

If you don't care about multi-platform, you can look into Runtime.exec

E.g:

Runtime.getRuntime().exec("cmd /c echo Hello World!");

If you do care, maybe you are better off doing the same thing from Java directly. Mostly shell commands can be achieved from Java directly without too much code. It will then be cross-platform and the performance will often be much better.

If you decide to go with invocation of shell commands anyway, to act on the output of it, learn how to access the standard input/output streams. Look at this example.

UPDATE

I think I now see that probably you want to write a program ./geniatagger that will accept input from the command line, e.g. output of 'echo' that was piped to it etc?

If so, look at using System.in, the standard input stream.

Stijn de Witt
  • 40,192
  • 13
  • 79
  • 80
  • No, please, no. At least, recommend using a `ProcessBuilder` instead of `Runtime.exec()` :/ – fge Mar 09 '14 at 20:43
  • Why? I use what works. Runtime.exec works. If he is willing to sacrifice 'write once, run anywhere' then a) it's not important enough to care, or b) the code he wants to invoke is very special / copyrighted / secret / whatever (I am assuming the 'echo' from the question is just an example) – Stijn de Witt Mar 09 '14 at 20:50
  • 1
    ProcessBuilder has better support for redirection of streams, better cmd/argument handling and requirements and generally makes it easier to use – MadProgrammer Mar 09 '14 at 20:53
  • Hello @MadProgrammer , I heard about ProcessBuilder I will try to read about that, could you please send me any example or site. – Peeyush Sahu Mar 09 '14 at 21:04
  • http://stackoverflow.com/questions/18995821/runtime-getruntime-execpath-to-file-runs-but-program-acts-strange/18995888#18995888 – MadProgrammer Mar 09 '14 at 21:46