468,545 Members | 1,901 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,545 developers. It's quick & easy.

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.getRuntime().exec(command);
int returnCode = process.waitFor();
System.out.println("Return code = " + returnCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}

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
Feb 25 '08 #1
2 2466
JosAH
11,448 Expert 8TB
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
Feb 25 '08 #2
Thanks! My code is working....
Feb 26 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Stefano Bianchi | last post: by
5 posts views Thread by Ayesha Ahsan | last post: by
4 posts views Thread by Jhuola Hoptire | last post: by
6 posts views Thread by Dave Kuhlman | last post: by
4 posts views Thread by Jean-Marc Blaise | last post: by
4 posts views Thread by Phil Campaigne | last post: by
11 posts views Thread by dolphin | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.