Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
Make sure that your Java program opens the serial port, allows time for the Arduino to reset before sending the first data and then keeps the serial port open until it is completely finished with the Arduino.
Robin2:
[...] allows time for the Arduino to reset before sending the first data and then keeps the serial port open until it is completely finished with the Arduino. [...]
Thanks! How easy the error may be if you do not respect the waiting!