Results 1 to 2 of 2
  1. #1
    shanky is offline Member
    Join Date
    Feb 2010
    Rep Power

    Default problems with executing unix commands

    Hello ppl,

    I'm trying to execute unix commands from java, but with weird results. Basically my unix command is a pipe and so I wrote a shell script and i'm trying to invoke it from java. This is the piece of code i use to invoke it

    cmd = new String[]{ "sh","./shell_script" }

    Runtime r = Runtime.getRuntime();
    Process p = r.exec(cmd);
    PrintWriter out = new PrintWriter(p.getOutputStream());
    InputStream error = p.getErrorStream();

    Now, my shell script contains a pipeline of commands and one of the command is that of an installed program, say mql. For instance., cat file_name | perl script1 | mql arg1 arg2

    My shell is able to recognize the mql command and when I run "sh./shell_script" from shell it runs fine. But when I invoke cmd from java, it gives an error, mql command not found. I tried to set the path in the shell configuration file and even added the whole path of the command in the shell script.

    I have a strong feeling that this is a problem with java. Could anyone throw some light on this?

    Thanks in advance,
    Last edited by shanky; 02-17-2010 at 07:11 PM.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Rep Power


    The problem is not the mql command, the problem is the pipes. When you exec a command in java (or in c for that matter), you are executing it without a shell. Symbols such as "|", "&", ">", etc. that you are used to when running commands in a shell will not work.

    To do what you are trying to do, you need to create a simple script that *does* use a shell, and exec that script. For example, create a script called 'example.csh" containing:

    Java Code:
    #!/bin/csh -f
    cat file_name | perl script1 | mql arg1 arg2
    ...and let 'cmd' be "example.csh".

Similar Threads

  1. Problems executing jar file outside of netbeans
    By 2potatocakes in forum New To Java
    Replies: 4
    Last Post: 04-29-2009, 09:53 PM
  2. Execute the commands in Jsp Program
    By swetha_2008 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-17-2008, 07:04 AM
  3. problems in running d command prompt commands
    By postaholic066 in forum Advanced Java
    Replies: 1
    Last Post: 09-10-2008, 03:47 PM
  4. Replies: 0
    Last Post: 06-20-2008, 09:02 AM
  5. Windows Runtime Commands
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-04-2008, 10:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts