Runtime.exec() just hangs

R

Robin Clarke

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>
 
R

Ray McVay

Robin said:
// 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?
 
R

Robin Clarke

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 said:
Robin said:
// 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?
 
W

William Brogden

Robin Clarke said:
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?
 
S

Sudsy

Robin said:
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.
 
R

Ray McVay

Robin said:
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 said:
Robin said:
// 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?
 
Joined
Jan 12, 2010
Messages
2
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top