Runtime.exec() just hangs

Discussion in 'Java' started by Robin Clarke, Sep 4, 2003.

  1. Robin Clarke

    Robin Clarke Guest

    Hi all,

    I am having trouble getting a response from the Runtime.exec()
    command. I am simply trying to execute a "df -k" command and display
    the output on a JSP (see below). When I run the page in Windows it
    works, when I run it in AIX it fails.

    I am using WebSphere 4.0.4 on AIX 5.1 (JDK 1.3.1).

    When I hit the page I see that a second java process is started (by
    doing a ps -ef from the command line), with the same parameters as the
    App Server that the page is in. When I load the JSP in IE, the page
    takes for ever to load. It does not time out or return an error.

    The code seems to hang when I try to read from the buffer - see my
    System.outs below.

    Does anyone have any ideas?

    Thanks
    robin



    I copied this code from -
    http://developer.java.sun.com/developer/TechTips/2000/tt0209.html#tip2


    Start of diskspace.jsp
    ------------------------------
    <%@ page import="java.io.*"%>
    <%@ page import="java.util.*"%>

    <%

    String[] cmd = new String[3];

    // Windows - works fine.
    // cmd[0] = "cmd.exe" ;
    // cmd[1] = "/C" ;
    // cmd[2] = "dir";

    // AIX - does not work
    cmd[0] = "/bin/sh";
    cmd[1] = "-c";
    cmd[2] = "/usr/bin/df -k";


    // set up list to capture command output lines

    ArrayList list = new ArrayList();

    // start command running

    Process proc = Runtime.getRuntime().exec(cmd);

    // get command's output stream and
    // put a buffered reader input stream on it

    InputStream istr = proc.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(istr));

    // read output lines from command

    System.out.println("gets here.");

    String str;
    while ((str = br.readLine()) != null) {
    list.add(str);
    System.out.println("DOES NOT get here.");
    }

    System.out.println("DOES NOT get here.");

    // wait for command to terminate

    try {
    proc.waitFor();
    }
    catch (InterruptedException e) {
    System.err.println("process was interrupted");
    }

    // check its exit value

    if (proc.exitValue() != 0)
    System.err.println("exit value was non-zero");

    // close stream

    br.close();

    %>

    <html>
    <head>

    </head>
    <body>

    <%
    for (int i = 1; i < list.size(); i++) {
    %>
    <%=(String) list.get(i)%>
    <%
    }
    %>
    </body>
    </html>

    ----------------------------
    End of diskspace.jsp
    Robin Clarke, Sep 4, 2003
    #1
    1. Advertising

  2. Robin Clarke

    Ray McVay Guest

    Robin Clarke wrote:
    > // AIX - does not work
    > cmd[0] = "/bin/sh";
    > cmd[1] = "-c";
    > cmd[2] = "/usr/bin/df -k";


    df is program, not a script, right? Have you tried executing it
    directly instead of spawning a shell?
    Ray McVay, Sep 5, 2003
    #2
    1. Advertising

  3. Robin Clarke

    Robin Clarke Guest

    How do I execute it directly? If I open a telnet window, and I execute
    "/usr/bin/df -k" I see what I expect to see.

    Are you telling me that I can only use Runtime.exec() to start scripts
    and not programs? I guess I am not sure what the difference is.

    Ray McVay <> wrote in message news:<bj8p9t$42ls$>...
    > Robin Clarke wrote:
    > > // AIX - does not work
    > > cmd[0] = "/bin/sh";
    > > cmd[1] = "-c";
    > > cmd[2] = "/usr/bin/df -k";

    >
    > df is program, not a script, right? Have you tried executing it
    > directly instead of spawning a shell?
    Robin Clarke, Sep 5, 2003
    #3
  4. "Robin Clarke" <> wrote in message
    news:...
    > Hi all,
    >
    > I am having trouble getting a response from the Runtime.exec()
    > command. I am simply trying to execute a "df -k" command and display
    > the output on a JSP (see below). When I run the page in Windows it
    > works, when I run it in AIX it fails.
    >
    > I am using WebSphere 4.0.4 on AIX 5.1 (JDK 1.3.1).
    >
    > When I hit the page I see that a second java process is started (by
    > doing a ps -ef from the command line), with the same parameters as the
    > App Server that the page is in. When I load the JSP in IE, the page
    > takes for ever to load. It does not time out or return an error.
    >
    > The code seems to hang when I try to read from the buffer - see my
    > System.outs below.
    >
    > Does anyone have any ideas?
    >
    > Thanks
    > robin
    >
    >
    >
    > I copied this code from -
    > http://developer.java.sun.com/developer/TechTips/2000/tt0209.html#tip2
    >
    >
    > Start of diskspace.jsp
    > ------------------------------
    > <%@ page import="java.io.*"%>
    > <%@ page import="java.util.*"%>
    >
    > <%
    >
    > String[] cmd = new String[3];
    >
    > // Windows - works fine.
    > // cmd[0] = "cmd.exe" ;
    > // cmd[1] = "/C" ;
    > // cmd[2] = "dir";
    >
    > // AIX - does not work
    > cmd[0] = "/bin/sh";
    > cmd[1] = "-c";
    > cmd[2] = "/usr/bin/df -k";
    >
    >
    > // set up list to capture command output lines
    >
    > ArrayList list = new ArrayList();
    >
    > // start command running
    >
    > Process proc = Runtime.getRuntime().exec(cmd);
    >
    > // get command's output stream and
    > // put a buffered reader input stream on it
    >
    > InputStream istr = proc.getInputStream();


    You may also have to consume the error stream from the process.
    proc.getErrorStream() ; // etc.

    Perhaps separate Threads to consume those streams is in order?





    >
    > BufferedReader br = new BufferedReader(new InputStreamReader(istr));
    >
    > // read output lines from command
    >
    > System.out.println("gets here.");
    >
    > String str;
    > while ((str = br.readLine()) != null) {
    > list.add(str);
    > System.out.println("DOES NOT get here.");
    > }
    >
    > System.out.println("DOES NOT get here.");
    >
    > // wait for command to terminate
    >
    > try {
    > proc.waitFor();
    > }
    > catch (InterruptedException e) {
    > System.err.println("process was interrupted");
    > }
    >
    > // check its exit value
    >
    > if (proc.exitValue() != 0)
    > System.err.println("exit value was non-zero");
    >
    > // close stream
    >
    > br.close();
    >
    > %>
    >
    > <html>
    > <head>
    >
    > </head>
    > <body>
    >
    > <%
    > for (int i = 1; i < list.size(); i++) {
    > %>
    > <%=(String) list.get(i)%>
    > <%
    > }
    > %>
    > </body>
    > </html>
    >
    > ----------------------------
    > End of diskspace.jsp
    William Brogden, Sep 5, 2003
    #4
  5. Robin Clarke

    Sudsy Guest

    Robin Clarke wrote:
    > How do I execute it directly? If I open a telnet window, and I execute
    > "/usr/bin/df -k" I see what I expect to see.
    >
    > Are you telling me that I can only use Runtime.exec() to start scripts
    > and not programs? I guess I am not sure what the difference is.


    Other way around: you can't start scripts by giving just the path, you
    have to specify an executable.
    Sudsy, Sep 5, 2003
    #5
  6. Robin Clarke

    Ray McVay Guest

    Robin Clarke wrote:
    > How do I execute it directly? If I open a telnet window, and I execute


    cmd[0] "/usr/bin/df -k";

    > "/usr/bin/df -k" I see what I expect to see.
    >
    > Are you telling me that I can only use Runtime.exec() to start scripts
    > and not programs? I guess I am not sure what the difference is.


    No, just that you only *require* a shell if you are going to exec shell
    scripts.

    > Ray McVay <> wrote in message news:<bj8p9t$42ls$>...
    >
    >>Robin Clarke wrote:
    >>
    >>>// AIX - does not work
    >>> cmd[0] = "/bin/sh";
    >>> cmd[1] = "-c";
    >>> cmd[2] = "/usr/bin/df -k";

    >>
    >>df is program, not a script, right? Have you tried executing it
    >>directly instead of spawning a shell?
    Ray McVay, Sep 6, 2003
    #6
  7. Robin Clarke

    vivek_kothari

    Joined:
    Jan 12, 2010
    Messages:
    2
    How To Give Input To Process Created By Runtime.getRuTime().exec

    Hi
    I am Trying to run a c program through JAVA. The C Program is

    #include <stdio.h>
    int main()
    {
    printf("Enter a number:\n");
    }

    and the JAVA code for invoking is

    import java.io.*;
    public class TimePass implements Runnable{
    Process pro;
    BufferedReader inputReaderForProcess;
    BufferedReader errorReaderForProcess;
    BufferedReader inputReaderForUser;
    Thread tProcessInput,tProcessError,tProcessUserInput;
    BufferedOutputStream wr;
    public TimePass(){
    tProcessInput=new Thread(this);
    tProcessError=new Thread(this);
    tProcessUserInput=new Thread(this);
    try {
    //pro=Runtime.getRuntime().exec("sh /home/globus/workspace/TimePass/bin/asd.sh");
    pro=Runtime.getRuntime().exec("/home/globus/my");
    /* try {
    pro.waitFor();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }*/
    } catch (IOException e) {
    e.printStackTrace();
    }
    tProcessInput.start();
    tProcessError.start();
    tProcessUserInput.start();
    }
    public static void main(String[] args) {
    new TimePass();
    }
    @Override
    public void run() {
    String line;
    if(Thread.currentThread()==tProcessError){
    errorReaderForProcess=new BufferedReader(new InputStreamReader(pro.getErrorStream()));
    try {
    while((line=errorReaderForProcess.readLine())!=null){
    line="ERROR > "+ line;
    System.out.println(line);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if(Thread.currentThread()==tProcessInput){
    inputReaderForProcess=new BufferedReader(new InputStreamReader(pro.getInputStream()));
    try {
    while((line=inputReaderForProcess.readLine())!=null){
    line="OUTPUT > "+ line;
    System.out.println(line);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if(Thread.currentThread()==tProcessUserInput){
    wr=new BufferedOutputStream(pro.getOutputStream());
    try {
    wr.write(0);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

    this works fine but if i add a scanf statement in the C program then the JAVA program hangs.

    Please help me out
    vivek_kothari, Jan 12, 2010
    #7
    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. Sergio

    Runtime.exec() hangs.

    Sergio, Jul 15, 2004, in forum: Java
    Replies:
    1
    Views:
    1,813
    Gordon Beaton
    Jul 15, 2004
  2. Hal Vaughan
    Replies:
    11
    Views:
    1,105
    Gordon Beaton
    May 22, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,076
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Vic
    Replies:
    16
    Views:
    2,013
  5. Tim Shadel

    Gem hangs => TCPSocket.write hangs

    Tim Shadel, Jul 23, 2005, in forum: Ruby
    Replies:
    1
    Views:
    381
    Ville Mattila
    Jul 24, 2005
Loading...

Share This Page