Java: Netbeans with jSerialComm

Following other Java topics and, as a completely new Java user, I was lost doing even the most simple thing, such as how to make a functioning project from a totally new installation of Netbeans IDE and Java JDK.

For anyone else equally ill informed, this is a step by step tutorial written as I was working out how to make the project work.

this is using MacOS, Netbeans 12.4 with jdk-16.0.2 and jSerialComm-2.7.0.jar to create a working test project that runs from terminal and sends/receives a message using the serial port, there should be no reason for the same procedure to be fully operational on linux/win too, I hope it might be useful to someone:-

How to use ‘jSerialComm-2.7.0.jar’ library with Netbeans 12.4 to make a functioning test project that can be run in Terminal on a Mac (and I expect without much change on any other platform).

the ‘jSerialComm-2.7.0.jar’ file is downloaded here:-

whichever download type you use it must be opened somewhere you will be able to reference in the IDE later on.

the Java Library used was ‘jdk-16.0.2.jdk’ from this link:-

this setup information adds the Netbeans part NOT shown in this link:-

these are the steps taken to get the project working, there should be no difference on ALL platforms, the IDE was a fresh install and had no changes to any of the default installed settings of any type.

it is possible to use other options but this was the tested method for this project.

1 open Netbeans, 12.4 (current at the time of writing)

2 go to FILE>NEW PROJECT, this opens a window called ‘New Project’

3 in ‘Categories:’ click ‘Java with Ant’, do not open the disclosure triangle

4 in ‘Projects:’ click ‘Java Application’, click ‘Next’ button

5 the ‘Name and Location’ window opens, set the PROJECT NAME> jSerialTest, the LOCATION and FOLDER will be filled with default locations, unless necessary, do not change these

6 ignore ‘Use Dedicated Folder for Storing Libraries’ it must be unticked

7 tick ‘Create Main Class’, this is filled automatically with ‘jserialtest.JSerialTest’, click ‘Finish’ button

8 the IDE creates all the project files needed for the framework of the test application and opens up the ‘’ tab in the main IDE window

9 click the ‘Projects’ ‘Libraries’ disclosure triangle, it should show one entry ‘JDK 16 (Default)’, if this is not the case then you may not have the JDK installed (which is beyond this scope to fix)

10 click the ‘Libraries’ folder to highlight it, then open menu with right click, select ‘Add JAR/Folder’, this opens ‘Add JAR/Folder’ window, now search for the location of the uncompressed file ‘jSerialComm-2.7.0.jar’, click the file when located, the information for the file is added to the text fields, accept the default settings and click ‘Open’, the file is added to the ‘Projects’ ‘Libraries’ folder in the IDE

11 next look at the ‘’ tab in the main IDE window, make space immediately ABOVE the code line ‘public class JSerialTest’, copy these two lines into that space:-

import java.nio.charset.StandardCharsets;
import com.fazecast.jSerialComm.SerialPort;

12 ignore any error flags at the moment, look at the line ‘// TODO code application logic here’, highlight this line with the thought that this line will be completely replaced by pasting the following code in its place:-

        byte[] readBuffer = new byte[2048];
		byte[] outputMessage = "Test Message to Transmit".getBytes(StandardCharsets.UTF_8);
		System.out.println("\nUsing jSerialComm Library Version v" + SerialPort.getVersion());
		SerialPort[] ports = SerialPort.getCommPorts();
		System.out.println("\nAvailable Ports:\n");
		for (int i = 0; i < ports.length; ++i)
			System.out.println("   [" + (i+1) + "] " + ports[i].getSystemPortName() + ": " + ports[i].getDescriptivePortName() + " - " + ports[i].getPortDescription());
		if (ports.length == 0) {
			System.out.println("   No Ports Detected!");
		SerialPort serialPort = ports[ports.length - 1];
		boolean openedSuccessfully = serialPort.openPort();
		System.out.println("\nOpening " + serialPort.getSystemPortName() + ": " + serialPort.getDescriptivePortName() + " - " + serialPort.getPortDescription() + ": " + openedSuccessfully);
		if (!openedSuccessfully)
		System.out.println("Setting read timeout mode to blocking with no timeout");
		serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
		System.out.println("\nWriting message to serial port: \"" + new String(outputMessage, StandardCharsets.UTF_8) + "\"");
		serialPort.writeBytes(outputMessage, outputMessage.length);
		System.out.println("Writing complete!\nReading message from serial port ");
		int numBytesRead = serialPort.readBytes(readBuffer, readBuffer.length);
		System.out.println("(" + numBytesRead + " bytes read): \"" + new String(readBuffer, StandardCharsets.UTF_8) + "\"");
		System.out.println("Reading complete!\n");
		System.out.println("Closing " + serialPort.getDescriptivePortName() + ": " + serialPort.closePort());

13 that is all the code required to test the serial port, it may be formatted in an odd way as it is just text, but the code should work fine.
next we will build it and then test it, you may notice the IDE has added two folders ‘Test Packages’ and ‘Test Libraries’ these can be ignored

14 now go to MENU>RUN>CLEAN AND BUILD PROJECT, or press the hammer+sweeping brush icon

15 the bottom of the IDE opens a tab called ‘Output’, after a short while it will print some lines of text, ending with ‘BUILD SUCCESSFUL (total time xx seconds)’, if not then you have done something wrong, restart and follow the instructions exactly

16 in the ‘Output’ tab there is a line ‘To run this application from the command line without Ant, try:’ followed immediately below is the text:-
java -jar “/Users/doj/NetBeansProjects/jSerialTest/dist/jSerialTest.jar”

17 this is the command that is used in ‘Terminal’ to run the application, open ‘Terminal’ on MacOS and paste the line from step 16 into it, then press ENTER

18 after a short pause while the ports are scanned you should get something like this:-

Using jSerialComm Library Version v2.7.0

Available Ports:

[1] cu.usbserial-FTB9GCLI: TTL232R-3V3 - TTL232R-3V3
[2] tty.usbserial-FTB9GCLI: TTL232R-3V3 (Dial-In) - TTL232R-3V3 (Dial-In)
[3] cu.Bluetooth-Incoming-Port: Bluetooth-Incoming-Port - Bluetooth-Incoming-Port
[4] tty.Bluetooth-Incoming-Port: Bluetooth-Incoming-Port (Dial-In) - Bluetooth-Incoming-Port (Dial-In)

Opening tty.Bluetooth-Incoming-Port: Bluetooth-Incoming-Port (Dial-In) - Bluetooth-Incoming-Port (Dial-In): true
Setting read timeout mode to blocking with no timeout

Writing message to serial port: “Test Message to Transmit”
Writing complete!
Reading message from serial port

on a Mac you will always have at least the Bluetooth port available, but if you are on another platform without any serial ports it will tell you none are available with a message “No Ports Detected!”.

so what my output tells us is that there are 2 actual ports on the system, for some reason MacOS has separate RX and TX references where on windows you might see COM1, COM3 etc.

the app is set in code ‘SerialPort serialPort = ports[ports.length - 1];’ to connect to the bluetooth serial port and the message tells us it has sent a message “Test Message to Transmit” to its TX buffer and is now sat, endlessly, waiting for a reply.

To test this I exited ‘Terminal’ to stop the app running and set up my MacBook Pro laptop with a terminal program (CoolTerm from this great place

on both laptop and iMac I opened ‘System Preferences’ and then ‘Bluetooth’, this showed the ‘Devices’ visible and after a short while both computers showed each other as connectable which is what I did.

next to the laptop, run CoolTerm, select ‘Options’ (Serial Port) ‘Port:’ ‘Bluetooth -Incoming-Port’, ‘Baudrate:’ 115200, click ‘OK’, the port is now connected and open.

now go to the other computer, paste the text from step 16 into ‘Terminal’ to run the app and, hey presto, the message “Text Message to Transmit” will appear in the CoolTerm window on the laptop.

the java app will now show ‘Reading message from serial port’ in the terminal window and it will be waiting forever for the reply.

on the laptop in CoolTerm go to the menu bar CONNECTION>SEND STRING and a small window opens up, type something in the ‘Send String’ window and click ‘Send’, that text SHOULD now appear in the other computers ‘Terminal’ window looking something like this:-

(11 bytes read): “hellooooooo”
Reading complete!

Closing MarksMacBookPro-Bluetoo (Dial-In): true [sic]
doj@Marks-iMac ~ %

the app closes down and releases the terminal back to the keyboard and the Bluetooth connection to the other computer is closed.

That is it, a complete test proving the system is working properly.

obviously the Bluetooth is not essential, it was used here for convenience as it was available.
the same functionality can also be had with 2 USB to serial converters and a serial crossover cable (or a home made cable with pins 2 and 3 of the 9 pin D crossed over and pin 5 (ground) joined up.

that can then be used on 2 separate machines or even on the same machine with CoolTerm used as above, 115200 appears to be the default baudrate for this app.

I would want to say: you can use also Azul Zulu Build of OpenJDK while it costs no license fee later. While Java SE from Oracle is free for development there is a license fee for production. With Azul OpenJDK 16 you can run also this project without problems. JDK builds for Production don’t have to be the same as for Development. Means: you can use Oracle for Development and OPENJDK for production and Vice versa. While I really like the Support of Azul I use always Azul Openjdk.

Ok Thorsten, good information.

I actually have this version on my machine, a link I was unable to find when I was writing the article above:-

when uncompressed it is in a folder ‘OpenJDK_16.0.2’, is this the same as the one you are referring to?

Openjdk is okay which Brand ever! Super. Good Job.

1 Like