Jssc arduino resets after serialPort.closePort()

Hello,
I tried to send data to the arduino, but it doesn't work, beacuse the led is switching off after 3 seconds.

Java code:

package Main;

import jssc.SerialPort;
import jssc.SerialPortException;

public class Main {
    public void main(String[] args) {
        SerialPort serialPort = new SerialPort("/dev/ttyUSB0");
        try {
            serialPort.openPort();//Open serial port
            Thread.sleep(3000);
            serialPort.setParams(9600, 8, 1, 0);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
            serialPort.writeBytes("a".getBytes());//Write data to port
            Thread.sleep(3000);
            serialPort.closePort();//Close serial port
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
        catch(InterruptedException e)  {
            System.out.println(e);
        }
    }
}

Arduino code:

const int LED_BUILTIN = 2;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  char i = Serial.read();
  if(i == 'a'){
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

The Arduino will reset each time the Serial link is opened.

But, it's reseting after I close it.

MrMugame:
But, it's reseting after I close it.

How do you know?

Are you saying it resets twice every time your Java program is run?

...R

How long does the Java code wait between closing the Serial port and opening it again ?

Sorry, When I run th code the led is turning on after 3 sec and turning off after 3 sec

As far as I can make out your Java program does this

opens the Serial port - this will reset the Arduino and turn off the LED
waits 3 seconds with the LED off
writes an 'a' to the Serial port - this will turn on the LED
waits 3 seconds with the LED turned on
repeats the four commands above

This exactly matches what you see on the Arduino
What happens if you only open the Serial port once and leave it open ?

When I let it open the same thing happens.And now my question is can I let the led on after closing the program?Is this possible?!

yes this is possible ! as far as i know there are 2 ways, (but probably more)
1- the automatic reset when opening the port can be disabled through a hardware modification (i have a switchable 10uF capacitor on the reset-pin and GND of my Mega (this disables reset, but also stops new sketches to be uploaded, hence the switch)
2 - you can use a separate USB <=> TTL and connect that to hwSerial (or a swSerial for that matter) It's the on board USB connection that sends a pulse to the reset pin.

MrMugame:
When I let it open the same thing happens.And now my question is can I let the led on after closing the program?Is this possible?!

Please post the Java code that only opens the Serial port once then leaves it open

I have never wanted to run a PC program that communicates with an Arduino and then closes down while leaving the Arduino program running so I was not sure what would happen.

I created a very slightly modified version of my Python - Arduino demo using a Mega so that it could send a message from setup() through Serial3 to a separate PC connection using a USB-TTL cable. That way I could see if the Mega was reset when the Python program closed.

And the result is that closing the Python program did not cause the Mega to reset.

...R