5 Replies - 494 Views - Last Post: 16 August 2011 - 06:01 AM Rate Topic: -----

#1 B13ZT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-August 11

Runtime.getRuntime(), ReadLine() doens't work

Posted 16 August 2011 - 04:27 AM

Hello Forum,

I have this code. The idea is that i send a command and i want to get the output in a stringbuffer that i then return.
The problem is that it works untill "line = in.readline()"
When i debug i receive that the application is running, but nothing happens.

Does anybody know what can be wrong?
Thank you :)

public StringBuffer executeSystemProcess(String process) {
        String line = null;
        StringBuffer param = new StringBuffer();
        BufferedReader in = null;
        try {
            Process proc = Runtime.getRuntime().exec(process);
            InputStreamReader inputStreamReader = new InputStreamReader(proc.getInputStream());
            in = new BufferedReader(inputStreamReader);
            line = in.readLine();
            while (line != null) {
                param.append(line);
                param.append('\n');
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (in != null)
                    in.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return param;
    }

This post has been edited by B13ZT: 16 August 2011 - 04:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Runtime.getRuntime(), ReadLine() doens't work

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Runtime.getRuntime(), ReadLine() doens't work

Posted 16 August 2011 - 04:38 AM

How do you expect the line to change at this loop:
line = in.readLine();
while (line != null) {
                param.append(line);
                param.append('\n');
            }
??
Was This Post Helpful? 2
  • +
  • -

#3 B13ZT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-August 11

Re: Runtime.getRuntime(), ReadLine() doens't work

Posted 16 August 2011 - 04:46 AM

View Postsmohd, on 16 August 2011 - 04:38 AM, said:

How do you expect the line to change at this loop:
line = in.readLine();
while (line != null) {
                param.append(line);
                param.append('\n');
            }
??


Thank you for your response. you are completly correct.
I changed it to "while ((line = in.readLine()) != null) {"
But the problem remains the same.
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Runtime.getRuntime(), ReadLine() doens't work

Posted 16 August 2011 - 05:26 AM

This should work, but some of the processes do not get to end(like cmd). To prove that it works try this:
 while ((line = in.readLine()) != null) {
                param.append(line);
                param.append('\n');
                System.out.println(param);//print what lines has been added
            }

For cmd, this loop will go 3 times and then go to an endless line!!
Was This Post Helpful? 1
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,216
  • Joined: 11-December 07

Re: Runtime.getRuntime(), ReadLine() doens't work

Posted 16 August 2011 - 05:28 AM

Put something like this in the loop so you can see what is going on.
System.out.println(">" + line + "<");

Maybe it's that there is always something to read in, even if it's an empty string.

lol. Ninjaed by smohd ;)
Was This Post Helpful? 2
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2111
  • View blog
  • Posts: 8,797
  • Joined: 20-September 08

Re: Runtime.getRuntime(), ReadLine() doens't work

Posted 16 August 2011 - 06:01 AM

See http://technojeeves..../52-runtimeexec

For the theory

http://www.javaworld...1229-traps.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1