Call Linux command from Java Application.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vaskarbasak
    New Member
    • Feb 2008
    • 27

    Call Linux command from Java Application.

    Hi all,

    i tried to run Linux command from Java Application.

    Here i past my code:-

    package com;

    public class linux_java {
    public static void main(String[] args) {
    try {
    String command = "cut -f 2,5 ABC/test.tab>ABC/test1.tab";
    final Process process = Runtime.getRunt ime().exec(comm and);
    int returnCode = process.waitFor ();
    System.out.prin tln("Return code = " + returnCode);
    } catch (Exception e) {
    e.printStackTra ce();
    }
    }
    }

    result:
    -------
    Return code =1

    But no new file was created.

    But when i run "cut -f 2,5 ABC/test.tab>ABC/test1.tab" command from Linux terminal it's works.

    please help me.

    Thanks!
    Vaskar
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    When you use your terminal to run that program it's the shell that takes care
    of your output redirection '>' Java just starts another process for you and that
    other process doesn't know anything about output redirection. That's why you
    can't find your output file (there isn't any).

    Either start up your shell using Java and pass that command line to it as
    arguments to your shell (check your man pages for the flags and syntax) or
    read from the other process' output stream and store the output in a file yourself.

    kind regards,

    Jos

    Comment

    • vaskarbasak
      New Member
      • Feb 2008
      • 27

      #3
      Thanks! My code is working....

      Comment

      Working...