Communication between Java and Arduino via COM3 port failed

I am fairly new to using Arduino, I also don't really understand how the COM ports work yet. I am trying to send data from my PC (via Java) to my Arduino, and I saw tutorials on YouTube that it can be done with Serial communication.

Both my Java code and Arduino code are somewhat mixed together from several tutorials (I will paste the source code below). The Arduino code "listens" to incoming data via the Serial port COM3, and the Java code tries to open the same port in order to send data through it. In theory (or as I understood it), the Java program should be able to send data, even if the Arduino is currently "listening" on the Serial port. But if I start the Java program after plugging in the Arduino and recompiling my Arduino code, the port seems to get "blocked" by the Arduino, so the Java program cannot open the port anymore.

This is the Arduino code:

void setup() {
  while (!Serial) {
    ; // wait for serial port to connect.
  Serial.println("Init done");

void loop() {
  if(Serial.available() > 0) {
    byte incomingByte = 0;
    incomingByte =;
    if (incomingByte != -1) {
      Serial.print("I got this ->");

and this is the Java code: (using the library jSerialComm-2.7.0)

import com.fazecast.jSerialComm.SerialPort;

public class SendData {

    public static void main(String[] args) {

        SerialPort sp = SerialPort.getCommPort("COM3");
        sp.setComPortParameters(9600, 8, 1, 0);
        sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);
        if (sp.openPort()) {
            System.out.println("Port is open :)");
        } else {
            System.out.println("Failed to open port :(");
        for (Integer i = 0; i < 5; ++i) {
            try {
                System.out.println("Sent number: " + i);
            } catch (IOException e) {
            try {
            } catch (InterruptedException e) {
        if (sp.closePort()) {
            System.out.println("Port is closed :)");
        } else {
            System.out.println("Failed to close port :(");

After running, the Java console outputs: "Failed to open port :("

My question is: How do I fix the code in order to make the communication work? Do I need to select some other port, include another library or try some completely different approach?
As I said, I'm really not experienced with Serial communication, so in my eyes, there could be a thousand reasons why this doesn't work, I just haven't found the right explanation online. If I left any crucial information out, let me know! I will gladly provide more information if necessary :slight_smile:

Which Arduino board and which operating system do you use ?
Most people use Python.

The serial port on the computer may not be used by the Arduino IDE or an other serial terminal program. You have to close the Arduino IDE.

@foerdiabi, your topic has been moved to a more suitable location on the forum.

Can you please edit your post, select the Arduino code and click </>. Repeat for the java code. Next save your post.

Are you sure? Serial Monitor, yes, IDE no. But the java application will interfere with code uploads by the looks of it.

No, not sure, but I'm taking no risks.
In the past I had some trouble with the serial port and Java. But I use linux, I don't know about Windows.

Wow, I really just had to close the Arduino IDE. Thank you very much! It works now :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.