• 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

Different Results, Same Code: Linux vs. DOS

 
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something wierd is happening happening with the following code (sorry, this page won't let me indent the code):
public class PrintInput
{
public static void main(String args[])
{
int i = 0;
String inputString = args[0] ;
for( i = 0; i < 100; i++ )<br /> {<br /> System.out.print(inputString + " ") ;<br /> }<br /> }<br /> }<br /> This code takes a string and prints it 100 times. Well it prints it 100 times when running the Java 1.2 under DOS. When the same code runs under Linux, it only prints it 77 times (consistently)!<br /> In other words: <br /> --> java PrintInput hello <-- at the DOS prompt prints "hello" 100 times.<br /> --> java PrintInput hello <-- at the Linux prompt prints "hello" 77 times.
What gives? Is something wrong with my code?
[This message has been edited by Matt Midcap (edited February 14, 1999).]
[This message has been edited by Matt Midcap (edited February 14, 1999).]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, be more specific. I tried Nikitin's JDK1.1.5 and the InputString was printed exactly 100 times. Say, how did you count the output ?
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use system.out.println instead and see if you get 100.
Also, a person can use HTML to display your code properly on these eporkchop pages
<PRE>
<PRE>
Your code goes here.
It works.
</PRE>

</PRE>


 
Matt Midcap
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did a "java -version" and got:
Kaffe Virtual Machine, bla, bla, bla,
Engine: Just-in-time Version: 1.00 Java Version 1.1
I did a UNIX word count (wc) to get the number. Actually here are the steps I did.
1. javac PrintInput.java
2. java PrintInput Hello > testFile
3. wc testFile
wc produced 77 words.
The odd thing (and maybe the key) is that if I "vi" the testFile, it says [Incomplete last line] at the bottom where the command line is.
Now I tried Michael's approach and used system.out.println and it worked just fine. wc produced 100 words.
Perhaps there is a "horizontal" printing limit? Any ideas?
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It could be that the output buffer is not flushing for some reason. Maybe the println forces a flush and print does not.
 
Pawel Veselov
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think so. The buffer is flushing when newline comes. I believe, Kaffe just suxx, when it printing characters to a single line.
So, I believe, that adding System.out.println() to the end of the program ( after the loop ) will not fix the problem.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is a buffer issue. PrintStreams automatically flush themselves whenever a line is terminated or a newline is written. Since no such event happens in the original code, there is no guarantee that the stream will be flushed at all. The differing results on the two systems is probably because some JVM's may flush open streams prior to ending.
I would wager similar behavior could be achieved with a FileStream. Open up a file, write some data to it, and quit the process without closing the stream. Data probably would be lost.
Brent Worden
http://www.Brent.Worden.org/
 
Matt Midcap
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think updating to the "best/latest" JDK wouldn't hurt.
I did a little research and found:
http://www.blackdown.org
They claim to have the "latest" JDK(1.1.7v1a) for Linux. Being a newbie I don't know what is best, latest or greatest.
I searches all over www.java.sun.com and found virtually nothing except articals on what other people are doing. Am I missing something on Sun's page - i.e. download Linux JDKx.x go here. ?
Any advise about upgrading my Linux JDK would be greatly appriciated! Thanks in advance.
[This message has been edited by Matt Midcap (edited February 16, 1999).]
 
Matt Midcap
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhhh, the saga continues....
Code:
<PRE>
public class PrintHello
{
public static void main(String args[])
{
System.out.print("Hello")
}
}
</PRE>
type in: java PrintHello Hello
DOS result: Hello
Linux result:
(nothing)
Replace with System.out.println and they both print Hello.
I'm a little disappointed with "Kaffe", not to mention my confidence level will suffer. I'll be asking myself "is it my code, or Kaffe".
I must admit, the buffer issue makes a lot of sence. Regardless, does anyone use a Linux JDK that works like it should?
[This message has been edited by Matt Midcap (edited February 16, 1999).]
[This message has been edited by Matt Midcap (edited February 16, 1999).]
 
Pawel Veselov
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, this looks like Kaffe doesn't do flush() on a output stream. I'll investigate Kaffe sources just after I download them.
And I have JDK, that works as it should, try ports of JavaSoft JDK, there are two mainteners - Sergey Nikitin and blackdown.org. I saw your message about the downloading - you miss nothing on JavaSoft page, they just don't support Linux ports.
 
Pawel Veselov
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, here it goes.
System.out is a BufferedOutputStream with FileOutputStream. BufferedOutputStream uses internal buffer with 2048 bytes to store the data something wrote to it. When the buffer overloads, or flush() called ( there is also writeBuffer() method, but it is private ), buffer really goes to FileOutputStream. And here goes why kaffe is really suxx.
Nobody calls finalize() methods for all existing classes. So, adding flush() to BufferedOutputStream.finalize() isn't a possible workaround. Actually, I can't find any place to hook this flush(). The way kaffe VM exits by System.exit() ( actually, Runtime.getRuntime().exit() ) and just end of program are differ too much. In case the program just ran to it's end, nothing is called, even Runtime.internalExit(), the only place I found libc exit() is called. So the only possible workaround is manually call flush() on all open BufferedXXXStreams. Weird.
One can hook out.flush() and in.flush() to System.exit(), but the you SHOULD use System.exit() to cancel the VM.
That's life

------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )

 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic