Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Runtime.exec() just hangs (http://www.velocityreviews.com/forums/t126369-runtime-exec-just-hangs.html)

Robin Clarke 09-04-2003 07:09 PM

Runtime.exec() just hangs
 
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/develo...0209.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

Ray McVay 09-05-2003 01:36 AM

Re: Runtime.exec() just hangs
 
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 09-05-2003 03:17 PM

Re: Runtime.exec() just hangs
 
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 <rmcvayNO@SPAMacm.org> wrote in message news:<bj8p9t$42ls$1@news.boulder.ibm.com>...
> 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?


William Brogden 09-05-2003 03:34 PM

Re: Runtime.exec() just hangs
 

"Robin Clarke" <rclarke@harrahs.com> wrote in message
news:a457858c.0309041109.19310e1c@posting.google.c om...
> 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/develo...0209.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




Sudsy 09-05-2003 08:53 PM

Re: Runtime.exec() just hangs
 
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.


Ray McVay 09-06-2003 01:37 AM

Re: Runtime.exec() just hangs
 
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 <rmcvayNO@SPAMacm.org> wrote in message news:<bj8p9t$42ls$1@news.boulder.ibm.com>...
>
>>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?



vivek_kothari 01-12-2010 11:17 AM

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())!=nul l){
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())!=nul l){
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


All times are GMT. The time now is 11:28 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.