• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading input from an external process line by line

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to use a sniffer called t-shark in my application. I managed to launch it succesfully using ProcessBuilder and I can read all the lines it sends to me, the problem is that it won't read it line by line. it just dumps everything to the screen after the program is done. Even if I wrap it in a thread, it still displays all the messages at once instead of giving me that - line by line. Here is the code (sorry if its a little messy, I am battling with this for two days and it won't run the way I want it to....)

Anyone has ideas?


code comments:
T-shark is a command line sniffer distributed with WireShark.
Timestamps are generated to see the difference between the time of packet arrival and its display time


T-shark parmeter description :
-i 3 - interface number on which the listening should take place. A list of interfaces is displayed with -D parameter
-c 10 - number of packets to be caught
-tad - every packet has a timestamp

 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please clarify. t-shark is a little utility that sends output to the screen in one large amount, I suppose via some kind of I/O stream, and then some other part of your application screen scrapes the output, or else intercepts the i/o stream, and you want to change the way t-shark makes the info available, is that what you are saying?

 
Kate Terlecka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:Could you please clarify. t-shark is a little utility that sends output to the screen in one large amount, I suppose via some kind of I/O stream, and then some other part of your application screen scrapes the output, or else intercepts the i/o stream, and you want to change the way t-shark makes the info available, is that what you are saying?



When I execute it off the command line it sends the output line by line, whenever the packet is captured. When I execute it from the java level it either sends everything at once (which is quite strange) or I am doing someting wrong to capture it at the time of arrival. Maybe there is another way to do that ?

Just to be clear: I tried executing everything with runtime.exec and I tried calling tshark via cmd /c. Every time outcome is the same.
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kate,

Have you read the article Why Runtime.exec() won't? It gives valuable insights on the gotchas of that call.
 
Kate Terlecka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martijn Verburg wrote:Hi Kate,

Have you read the article Why Runtime.exec() won't? It gives valuable insights on the gotchas of that call.



Yes I have, and I tried to run everything the way it's explained there, but no luck. What is weird is that all the programs I tried to run give me whole output at once on the InputStream, but ErrorStream goes real-time ... and I can't redirect it except for processBuilder.redirectErrorStream(true) , but it makes no diference to the InputStream ..
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kate Terlecka wrote:

Fred Hamilton wrote:Could you please clarify. t-shark is a little utility that sends output to the screen in one large amount, I suppose via some kind of I/O stream, and then some other part of your application screen scrapes the output, or else intercepts the i/o stream, and you want to change the way t-shark makes the info available, is that what you are saying?



When I execute it off the command line it sends the output line by line, whenever the packet is captured. When I execute it from the java level it either sends everything at once (which is quite strange) or I am doing someting wrong to capture it at the time of arrival. Maybe there is another way to do that ?

Just to be clear: I tried executing everything with runtime.exec and I tried calling tshark via cmd /c. Every time outcome is the same.



OK, I had a few ideas, but they really don't apply in this scenario. Good luck with it.
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad you've read the article, it's the number one path we send people down . So looking more closely at your code is it safe to say that the:

line = reader.readLine() statement gives you all of the output in that one 'line'?

Have you tried alternative methods to reading in that output (e.g. Other methods with the Java I/O API)?
 
Kate Terlecka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martijn Verburg wrote:Glad you've read the article, it's the number one path we send people down . So looking more closely at your code is it safe to say that the:

line = reader.readLine() statement gives you all of the output in that one 'line'?

Have you tried alternative methods to reading in that output (e.g. Other methods with the Java I/O API)?



I can say that I get my output in one "line" - here is the sample output:



you can see clearly from the timestamps that is written to the output after everything is captured.

I only tried different reader combinations, reading single signs and casting chars and so on, but still the same output. If there is some other way to caputre the stream I'll be glad to hear about it
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On second thought...

In my java project I deal with a situation where I want to have my java GUI communicate with a number of different applications that can be of different forms, i.e. they can be threaded java classes, windows .exe files, anything. The accepted protocols for this communication uses standard I/O. Anyways I have a fair bit of info on this.

I had a thread in the I/O and Streams forum that went nowhere. If you think my situation applies to yours, then I can probably provide some useful information.

https://coderanch.com/t/446572/Streams/java/using-Standard-Input-Ouput-different
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kate,

Can I ask what JDK you are using? I'll try and research some options for you.
 
Kate Terlecka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martijn Verburg wrote:Hi Kate,

Can I ask what JDK you are using? I'll try and research some options for you.



1.6. Thanks!
 
Kate Terlecka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:On second thought...

In my java project I deal with a situation where I want to have my java GUI communicate with a number of different applications that can be of different forms, i.e. they can be threaded java classes, windows .exe files, anything. The accepted protocols for this communication uses standard I/O. Anyways I have a fair bit of info on this.

I had a thread in the I/O and Streams forum that went nowhere. If you think my situation applies to yours, then I can probably provide some useful information.

https://coderanch.com/t/446572/Streams/java/using-Standard-Input-Ouput-different



Looks like that might be something concerning the command line interpreter, but still it's hard to say what might be the cause, as error stream behaves completely different than input stream ... Honestly I'm out of ideas what that might be ..
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again Kate,

I'm curious, have you tried process.getOutputstream() and seeing what that gives you?
 
Kate Terlecka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martijn Verburg wrote:Hi again Kate,

I'm curious, have you tried process.getOutputstream() and seeing what that gives you?



what do you mean? I can write to the output, but tshark does not take any input during the running time ..
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry had a brain melt there, ignore that last post , Have you've tried the various read operations on InputStreamReader before assigning it to an BufferedReader?

See if you can get the individual bytes and detect the \r, \n or \r\n pattern (newline)

That's about all I've got to try unfortunately...
 
Kate Terlecka
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody for the replies - I found the way. I came out that one of the configuration options of tshark, enable line by line capturing. I wrote to the tshark support and they told me that it' s no java's fault. Anyway almost all the programs giving real-time output dump it all in the end, unless you specify you have to get it char by char. That's what made me think it's java. This will be a good lesson for others

Thanks again,
Kate
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad you got it solved! it certainly had me scratching my head wondering if I should go back to Java 101 ;)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic