Reading Serial data from java / jSerial causes Aduino to crash

Hello everyone,
after getting started with some easier projects for my Arduino I decided to move on to something a bit more difficult.
As I have already done some programming in Java I tried interfacing my Arduino with a simple java script using the jSerial library (GitHub - Fazecast/jSerialComm: Platform-independent serial port access for Java). Sending data from the Arduino and parsing it in java works just fine, but if I try sending data to the Arduino, the Arduino always crashes, my Java script works just fine. The crash seems to occur completely independent of the sketch running on the Arduino. Even the blink example sketch crashes as soon as I send a byte from my java script, judging from the LEDs the arduino restarts immediately after receiving the first byte. I´ve also tried out another Arduino, but I´m facing the same problem on my Arduino Uno, as well as on a cheap nano clone. If I had to guess, I´d say that the bootloader is somehow responding to the serial data, but I´m way to inexperienced to actually figure out whats going on.

I´m using the following code in my Java script:

import java.io.IOException;
import com.fazecast.jSerialComm.*;

public class MainClass {

	public static void main(String[] args) throws IOException, SerialPortInvalidPortException{
		SerialPort myPort = null;
		byte i = 123;
		try {
			myPort = SerialPort.getCommPort("COM6");
			System.out.println(myPort.getSystemPortName());
			myPort.setComPortParameters(9600, 8, 1, 0);
			myPort.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);
			while (!myPort.isOpen()) {
				if (myPort.openPort()) {
					System.out.println("Opend Port");
				}
				else {
					System.out.println("Failed to open port");
				}
			}
			myPort.getOutputStream().write(i);
			myPort.getOutputStream().flush();
			System.out.println("Wrote tesbyte");
		}
		finally {
			myPort.closePort();
		}
	}
}

I hope I´m in the right forum for this issue as the problem seems to lie more on the java sight of the programming.
I´m looking forward to your support because to be honest I´ve become quite frustrated. The tutorials I´ve been following and the documentation of the library have offered no help for this kind of problem, there also aren´t a lot of projects using java for interfacing.
Thanks for all your efforts in helping me!

Please post your Arduino program.

How do you know the Arduino crashes?

I suspect the problem is that your Java program is opening and closing the serial port. It should open the serial port, allow time for the Arduino to reset and then keep the serial port open until it is completely finished with the Arduino.

Have a look at how I do it in this Simple Python - Arduino demo

...R

You were absolutely right, if I delay the Java program for a couple seconds everything works as it should.
I knew that for the first second or so the Arduino was blinking in exactly the same way it does when plugged in. Furthermore I knew that the setup() part of the code was getting executed. This is why I just assumed that the Arduino had crashed and therefore restarted.
But as it turns out the Arduino just takes some time to create the serial connection and could not receive the bytes sent.
Thank you so much for your support, you absolutely saved my project!!!