running unix script from java app

Discussion in 'Java' started by sight, Jun 16, 2006.

  1. sight

    sight Guest

    String[] cmd = {"/bin/sh", "-c", "ls > hello"};
    Runtime.getRuntime().exec(cmd);
    Following code doesn't create a file hello when app is run on unix?!??How
    can i start unix script from java app?I'm using cygwin instead of Unix
     
    sight, Jun 16, 2006
    #1
    1. Advertising

  2. sight

    Oliver Wong Guest

    "sight" <> wrote in message
    news:e6tope$oor$...
    > String[] cmd = {"/bin/sh", "-c", "ls > hello"};
    > Runtime.getRuntime().exec(cmd);
    > Following code doesn't create a file hello when app is run on unix?!??How
    > can i start unix script from java app?I'm using cygwin instead of Unix


    Did you try:

    String[] cmd = {"/bin/sh", "-c", "ls" "> hello"};

    or

    String[] cmd = {"/bin/sh", "-c", "ls" ">" "hello"};

    or other variants? I believe it's the shell and not the ls program, which
    takes care of redirecting output.

    - Oliver
     
    Oliver Wong, Jun 16, 2006
    #2
    1. Advertising

  3. sight wrote:
    > String[] cmd = {"/bin/sh", "-c", "ls > hello"};
    > Runtime.getRuntime().exec(cmd);
    > Following code doesn't create a file hello when app is run on unix?!??How
    > can i start unix script from java app?I'm using cygwin instead of Unix
    >
    >


    I think that the problem is with "ls > hello" part. Runtime.exec()
    method creates a Process object instance. Java captures stdin,
    stdout and stderr streams, so you are not able to redirect. The
    command is probably executed, but you are not able to see the result.

    Check the java.lang.Process documentation.

    DG
     
    =?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=, Jun 16, 2006
    #3
  4. sight

    Chris Uppal Guest

    sight wrote:

    > String[] cmd = {"/bin/sh", "-c", "ls > hello"};
    > Runtime.getRuntime().exec(cmd);
    > Following code doesn't create a file hello when app is run on unix?!??How
    > can i start unix script from java app?I'm using cygwin instead of Unix


    Your approach would, I think, be correct on Unix. You are passing the right
    string to the shell to be interpreted as a command. I suspect the problem is
    with Cygwin, or more accurately, with the fact that Java doesn't know about
    Cygwin. So the first parameter:
    "/bin/sh"
    with attempt to find
    .\bin\sh.exe
    in a subdirectory of whatever "current directory" the Windows process thinks it
    is in. You may get better results with:
    "C:/cygwin/bin/sh"
    (replacing the actual path according to where you have actually installed
    Cygwin).

    -- chris
     
    Chris Uppal, Jun 16, 2006
    #4
  5. Oliver Wong wrote:
    > or other variants? I believe it's the shell and not the ls program,
    > which takes care of redirecting output.


    That is, in fact, correct. In UNIX, child process, by default, inherits
    parents file descriptors. That means that paren program arranges the
    descriptors before forking child process.

    Check dup, dup2, open and close system calls.

    DG
     
    =?ISO-8859-2?Q?Dra=BEen_Gemi=E6?=, Jun 16, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,831
  2. Stefan Ram

    lauching java app over ssh on unix

    Stefan Ram, Feb 12, 2006, in forum: Java
    Replies:
    4
    Views:
    609
    Richard Wheeldon
    Feb 12, 2006
  3. mwmann
    Replies:
    8
    Views:
    2,534
    mwmann
    Sep 14, 2006
  4. Replies:
    3
    Views:
    536
    Daniel Pitts
    Jan 3, 2007
  5. Les
    Replies:
    1
    Views:
    96
Loading...

Share This Page