Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Piping println() through an external pager program (more, less)

Reply
Thread Tools

Piping println() through an external pager program (more, less)

 
 
Thomas Kellerer
Guest
Posts: n/a
 
      03-14-2009
Hi,

I have a console application ("commandline") that is writing text to the console
using System.out.println (I'm *not* referring to the "Java Console" that is
started when an applet is started)

I would like to support paging of the output, but I can't find a way to do it.

My first approach was to try to find the current size of the console, but this
does not seem to be possible in a cross-platform manner (in pure Java)

So I thought I could simply write my output to a spawned "pager" process ("print
ln() | more") but I can't get this to work either.

I tried the example from http://mindprod.com/jgloss/exec.html and even though
the output is shown on screen, the pager (in my case "more" under Windows) does
not kick in and I don't see the prompt to press enter to see the next page.

I tried several combinations of using Process.getInputStream() and
Process.getOutputStream() (and mingling with System.out and System.in) but
either I can't see the output or the output is not paged.

Any ideas?

Thanks
Thomas

 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      03-14-2009
In article <(E-Mail Removed)>,
Thomas Kellerer <(E-Mail Removed)> wrote:

> I have a console application ("commandline") that is writing text to
> the console using System.out.println (I'm *not* referring to the
> "Java Console" that is started when an applet is started)
>
> I would like to support paging of the output, but I can't find a way
> to do it.
>
> My first approach was to try to find the current size of the console,
> but this does not seem to be possible in a cross-platform manner (in
> pure Java)
>
> So I thought I could simply write my output to a spawned "pager"
> process ("print ln() | more") but I can't get this to work either.
>
> I tried the example from http://mindprod.com/jgloss/exec.html and
> even though the output is shown on screen, the pager (in my case
> "more" under Windows) does not kick in and I don't see the prompt to
> press enter to see the next page.
>
> I tried several combinations of using Process.getInputStream() and
> Process.getOutputStream() (and mingling with System.out and
> System.in) but either I can't see the output or the output is not
> paged.


Why not pipe the output of CommandLine to `more` or less`?

$ java CommandLine | more

You can always execute a process containing such a command, or have the
user invoke a suitable shell/batch command.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
 
 
 
Thomas Kellerer
Guest
Posts: n/a
 
      03-14-2009


John B. Matthews wrote on 14.03.2009 18:58:
> In article <(E-Mail Removed)>,
> Thomas Kellerer <(E-Mail Removed)> wrote:
>
>> I have a console application ("commandline") that is writing text to
>> the console using System.out.println (I'm *not* referring to the
>> "Java Console" that is started when an applet is started)
>>
>> I would like to support paging of the output, but I can't find a way
>> to do it.
>>
>> My first approach was to try to find the current size of the console,
>> but this does not seem to be possible in a cross-platform manner (in
>> pure Java)
>>
>> So I thought I could simply write my output to a spawned "pager"
>> process ("print ln() | more") but I can't get this to work either.
>>
>> I tried the example from http://mindprod.com/jgloss/exec.html and
>> even though the output is shown on screen, the pager (in my case
>> "more" under Windows) does not kick in and I don't see the prompt to
>> press enter to see the next page.
>>
>> I tried several combinations of using Process.getInputStream() and
>> Process.getOutputStream() (and mingling with System.out and
>> System.in) but either I can't see the output or the output is not
>> paged.

>
> Why not pipe the output of CommandLine to `more` or less`?
>
> $ java CommandLine | more


Thanks for the answer

Piping from the commandline is not possible because the application is
interactive. The user can enter commands and the output of the commands should
be be paged if the user wants that.

Thomas
 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      03-14-2009
Thomas Kellerer wrote:
> I would like to support paging of the output, but I can't find a way to
> do it.


This is typically done using a special terminal support library called
"curses" or one of its derivatives. Googling "Java curses" would prove
helpful.

> So I thought I could simply write my output to a spawned "pager" process
> ("print ln() | more") but I can't get this to work either.


Runtime.exec() is not a shell. Even if the piping would work, the more
program would not recognize the Java caller as a terminal and would
therefore merely act like the program cat.

> Any ideas?


From your later comments, it seems you most want to look into a Java
curses library.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      03-14-2009
Thomas Kellerer wrote:

>
> Thanks for the answer
>
> Piping from the commandline is not possible because the application is
> interactive. The user can enter commands and the output of the commands
> should be be paged if the user wants that.



You'll almost certainly need some JNI calls to determine the screen
size.. and to detect when it changes too.

Command line programs that are interactive and work "like windows" are
passe at this point, imo. I'm not surprised Sun has not invested any
effort into making them convenient to do in Java.

Have you considered C/C++ and native libs? Any given OS is likely to
support several types of command windows -- good luck actually getting
them all working.

I'd require the user to support and run a GUI so Swing/AWT can be used.
Anything else is just too far beyond the pale these days. Even
servers, I've run into configuration tools that required
X-Windows/KDE/etc. The IT folks who do servers I talk to say the same
-- GUI required for servers these days. And probably have been required
for at least 5 years or so. Tell you customer to get with the program,
or make do with an inferior product, or be prepared to pay a lot to make
up the difference.

 
Reply With Quote
 
Jon Gomez
Guest
Posts: n/a
 
      03-17-2009
Mark Space wrote:
> You'll almost certainly need some JNI calls to determine the screen
> size.. and to detect when it changes too.


Alternatively, the user could supply arguments to the program to
manually set the screen size. Maybe not a very self-adapting solution,
but "portable".

> Command line programs that are interactive and work "like windows" are
> passe at this point, imo. I'm not surprised Sun has not invested any
> effort into making them convenient to do in Java.


I guess I have a different opinion, but I respect yours.

> Have you considered C/C++ and native libs? Any given OS is likely to
> support several types of command windows -- good luck actually getting
> them all working.
>
> I'd require the user to support and run a GUI so Swing/AWT can be used.
> Anything else is just too far beyond the pale these days. Even
> servers, I've run into configuration tools that required
> X-Windows/KDE/etc. The IT folks who do servers I talk to say the same
> -- GUI required for servers these days. And probably have been required
> for at least 5 years or so. Tell you customer to get with the program,
> or make do with an inferior product, or be prepared to pay a lot to make
> up the difference.


Sounds quite annoying. I would prefer servers for which you can alter
..conf files directly , but I imagine they may not have any choice.

Jon.
 
Reply With Quote
 
Thomas Kellerer
Guest
Posts: n/a
 
      03-17-2009
Hi,

Mark Space, 14.03.2009 20:41:
> Command line programs that are interactive and work "like windows" are
> passe at this point, imo. I'm not surprised Sun has not invested any
> effort into making them convenient to do in Java.

I'm not trying to simulate a windowing environment on the text console, all I want to do is give the user the ability to page through the result of a SQL statement.
Fn fact my application does have a Swing GUI, but sometimes there is no way of having an X-Window (e.g. when using SSH where you can't forward the necessary ports.

> Have you considered C/C++ and native libs? Any given OS is likely to
> support several types of command windows -- good luck actually getting
> them all working.

No, as I want a portable Java only solution

Thomas
 
Reply With Quote
 
Thomas Kellerer
Guest
Posts: n/a
 
      03-17-2009
Jon Gomez, 17.03.2009 02:45:
> Alternatively, the user could supply arguments to the program to
> manually set the screen size. Maybe not a very self-adapting solution,
> but "portable".


Yes, that's probably where I end up. Not nice, but workable.

I actually do have a solution for windows (parsing the output of the mode command) but I haven't found a solution for *nix. Parsing the output of the stty command does not seem to work as the spawned process is probably not associated with a terminal

I tried to retrieve the screen size from within a shell script because interactively $LINES and $COLUMNS are available (echo $LINES shows the correct value) but when trying to retrieve them using System.getenv() it returns null. I guess my workaround will be to pass those variables as System properties in my shell script starting the application. This will not reflect changes the user does to the window while my app is running but it's a good starting point.

Thanks
Thomas
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      03-17-2009
Thomas Kellerer <(E-Mail Removed)> wrote:
> I would like to support paging [portions] of the output
> [of an interactive program], but I can't find a way to do it.


from skimming through this thread, I have the impression you're
not yet satisfied with the answers so far.

Here's another one:
I had a slightly related problem of doing tty-stuff in a Java
text("console")-app, where I needed to turn off the tty-echo,
and the new password-api wasn't an Option.

This was like how I got "stty" to see the console:
String sttyCmd= on ? "echo" : "-echo";
Runtime.getRuntime().exec(new String [] {
"sh", "-c", "stty "+sttyCmd+" </dev/tty"
}).waitFor();

If you redirect output and error the same way to the same device,
then I'd expect that you could even start "less" this way. (It may
suffice redirecting only stdout for less to make it use the tty
and feed the data per stdin, but I'd recommend saving all the data
in a file first, and then starting less with the filename as argument
and stdin,out,err all tied to /dev/tty. In that case, make sure, you
use a tmpfilename that doesn't contain shell-meta characters, or you
will need to quote them for the shell.)

Sorry, not a really platform-agnostic answer, either, but you
seemed to already have a solution for windows, anyway

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-17-2009
Thomas Kellerer wrote:
> Fn fact my application does have a Swing GUI, but sometimes there is no
> way of having an X-Window (e.g. when using SSH where you can't forward
> the necessary ports.


Are you speaking of firewall settings and such?

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
piping input to an external script Tim Arnold Python 9 05-12-2009 05:24 PM
Java: Using Piping or any other methods to use the output from one program as input in another. julielaurek@gmail.com Java 12 07-04-2007 01:06 AM
piping data stream through GPG robert Python 2 06-09-2007 04:50 PM
Piping external commands saibotorama@gmail.com Python 1 07-18-2006 05:43 PM
Piping binary data to an external program |MKSM| Ruby 1 03-06-2006 09:44 AM



Advertisments