• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get grep output in JSP  RSS feed

 
Jd Steven
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Could someone please help me with how to get Unix command output in my JSP file? I am able to execute a unix command from JSP. However, I need to display the output in my JSP and I am not able to do it. This is what I am doing in my JSP -



Thanks in advance!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When exec()ing, you MUST consume the stdOut and stdErr streams created by the process.

If you don't the process will probably hang indefinately.

The JavaDocs for java.lang.Process should have been your first stop when researching this question.

See also the classic artice on exec() here.

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is much more about exec() and not so much about JSP, moved to a more appropriate forum.
 
Jd Steven
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out.

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not going to work. The p.waitFor() call will block until you clear out the InputStream. You only start doing that afterwards though.
 
Jd Steven
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it does work fine. Thanks.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider yourself lucky. Apparently your process' buffer has not been filled yet. If it writes more to its output stream the buffer will be filled, and because you do not clear it until after the call to waitFor() that call will then block indefinitely. Read the article William linked to, really.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, there are plenty of pitfalls here.

As Rob pointed out the fact that it worked once means only that you got lucky on the size of the Process output buffer. Also, if for some reason the process writes to the stdErr stream the program will likely hang indefinately.

Bill
 
Jd Steven
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the link to article. It was pretty good.

Bill, on process hanging for infinite time if it writes to stdErr, that doesn't happen. I tested it and all I had to do was instead of getting inputStream get errorStream to process error response. So one block for input stream and one block for error stream.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So one block for input stream and one block for error stream.


I would swear that was exactly what I was trying to tell you - where did I go wrong?

For best result you need separate Threads to consume both stdOut and stdErr streams just in case a more complex process produces a mixture of both.

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!