Crazy Java Problem Involving AIX

(hctunx04)/home/cad8278-->java -version

java version"1.4.1"

Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1)

Classic VM (build 1.4.1, J2RE 1.4.1 IBM AIX build ca1411sr2b-20040520 (JIT enabled: jitc))

Okay. what I'm trying to accomplish:

I'm trying to run a command in unix, and chew on that data in java. Simple, right?

The problem I'm running into:

When I'm reading in the inputStream, I'm only getting parts of the data:

This is the output I get when running the command in unix:

(hctunx04)/home/cad8278-->ls | grep txt

24011C.rmd07015.txt.0160246

24030C.rmd07015.txt.0160246

output.txt

output2.txt

output3.txt

possibleDays.txt

test.txt

tree.txt

treedata.txt

This is what prints out when I run my code:

24030C.rmd07015.txt.0160246

output2.txt

possibleDays.txt

tree.txt

null

Here is the code that I'm running:

Runtime rtime = Runtime.getRuntime();

Process child = rtime.exec(new String[]{"/bin/bsh","-c", getCommand()});

//set up the InputStream reader

InputStreamReader isr =new InputStreamReader(child.getInputStream());

BufferedReader br =new BufferedReader(isr);

while (br.readLine()!=null){

sb.append(br.readLine() +"\n");

}

Okay, so I'm getting intermittent data... and a null... odd.

If I change the code to this:

Runtime rtime = Runtime.getRuntime();

Process child = rtime.exec(new String[]{"/bin/bsh","-c", getCommand()});

//set up the InputStream reader

InputStreamReader isr =new InputStreamReader(child.getInputStream());

BufferedReader br =new BufferedReader(isr);

for(int i = 0; i < 30; i++)

{

sb.append(br.readLine() +"\n");

}

Then my output is:

24011C.rmd07015.txt.0160246

24030C.rmd07015.txt.0160246

output.txt

output2.txt

output3.txt

possibleDays.txt

test.txt

tree.txt

treedata.txt

null

null

null

[.. many nulls]

null

null

I get all of data... but I'm not sure how to stop my loop.

BTW, if I change my for loop to:

for(int i = 0; i < 30; i++)

{

if(br.readLine()!=null)

sb.append(br.readLine() +"\n");

}

I get the same results as the while loop.

Any advice would be much, much appreciative.

--Adam

Message was edited by:

TundraWolf_

[3585 byte] By [TundraWolf_a] at [2007-11-27 11:58:54]
# 1

> Any advice would be much, much appreciative.

>

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Read, then read again then implement the recommendations.

sabre150a at 2007-7-29 19:22:35 > top of Java-index,Java Essentials,Java Programming...
# 2

Very good article on Runtime.exec.

I figured out my problem, here is the code change I made:

StringBuffer sb = new StringBuffer();

String appendLine = "";

try{//must handle IOExceptions

Runtime rtime = Runtime.getRuntime();

Process child = rtime.exec(new String[] { "/bin/bsh","-c", getCommand()});

//set up the InputStream reader

InputStream ins = child.getInputStream();

InputStreamReader isr = new InputStreamReader(ins);

BufferedReader br = new BufferedReader(isr);

while ((appendLine = br.readLine())!=null){

System.out.println(appendLine);

sb.append(appendLine + "\n");

}

Now, as to why checking if my bufferReader readLine() is null, or a string set to the same value and checking that for null gives me a different result is beyond me. I'll have to play with it a bit.

thanks!

--Adam

TundraWolf_a at 2007-7-29 19:22:35 > top of Java-index,Java Essentials,Java Programming...
# 3

I think you are falling for one of the traps ( the process buffer(s) filling) so I think you need to read the article again and handle stderr and stdout properly.

sabre150a at 2007-7-29 19:22:35 > top of Java-index,Java Essentials,Java Programming...