List all the serial and parallel ports in your system using Java

By: Ashish  

Are you getting javax.comm.NoSuchPortException error? If yes, then first check if ur COM port is really been detected by the driver. I'll give you one program that will display all the serial and parallel ports in your system.

/* program*/

import javax.comm.*;
import java.util.Enumeration;

public class ListPorts {
public static void main(String args[]) {
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements()) {
CommPortIdentifier port = (CommPortIdentifier)ports.nextElement();
String type;
switch (port.getPortType()) {
case CommPortIdentifier.PORT_PARALLEL:
type = "Parallel"; 
break;
case CommPortIdentifier.PORT_SERIAL:
type = "Serial"; 
break;
default: /// Shouldn't happen
type = "Unknown"; 
break;
}
System.out.println(port.getName() + ": " + type);
}
}
}

just check it ...
this is what the output will be!!( my case)

COM1: Serial
COM2: Serial
COM7: Serial
LPT1: Parallel
LPT2: Parallel




Archived Comments

1. I found javax.comm not really fit for my project and I now use this library https://github.com/Rishi
View Tutorial          By: David at 2015-05-04 18:06:14

2. Hello,
I am not getting any output.
It return the null for
Enumeration ports =

View Tutorial          By: Niket DOshi at 2015-05-01 03:03:49

3. Hi
I tried to use this class , I don't get any errors neither any out put
plz help m

View Tutorial          By: arnav at 2015-03-27 06:01:22

4. Having this error while using comm.jsr for sending sms

javax.comm.NoSuchPortException

View Tutorial          By: Amit Kumar at 2013-10-15 09:04:58

5. hi everybody....

please use comm.jar file .CommPortIdentifier is a class in the ja

View Tutorial          By: manoj at 2012-09-24 10:41:34

6. hey usharadha and all other friends...i hv taken a project in which this sending and receiving of sm
View Tutorial          By: abdur at 2012-02-03 15:56:10

7. Hi
I tried to use this class , I don't get any errors neither any out put
plz help m

View Tutorial          By: Asela at 2012-01-26 19:05:14

8. Sir, this code not giving any output.
it is executed sucessfully but no list generated

View Tutorial          By: Deepak at 2011-03-19 23:39:49

9. I changed import javax.comm.*; for this line import gnu.io.CommPortIdentifier

and I'

View Tutorial          By: Julen at 2010-07-18 18:39:29

10. I changed import javax.comm.*; for this line import gnu.io.CommPortIdentifier

and I'

View Tutorial          By: Julen at 2010-07-18 18:39:25

11. I had just copied the code & tried to execute under WinXP SP2
it i got mesage
---

View Tutorial          By: dhananjay padalkar at 2010-02-13 19:20:27

12. Sir,
i am implementing the above code in windows xp,
and the following error is occur

View Tutorial          By: Girish padki at 2010-01-15 21:50:50

13. Support for serial ports is platform dependent and Sun removed support for serial ports for Windows
View Tutorial          By: Sal at 2009-09-15 15:51:55

14. HI, like some guys in this tutorial I get this problem from java:
---------------------------

View Tutorial          By: omar carpio at 2009-09-06 13:17:37

15. Sir,
i am implementing the above code in windows xp,
and the following error is occur

View Tutorial          By: Rahul Singh at 2009-09-01 04:23:25

16. Can anyone provide me the Java code to read SMS using GSM modem.I'd be grateful to him or her.
View Tutorial          By: Jaydatta at 2009-04-06 00:21:09

17. By running ListPorts, I am not getting any output and while running smsCliend I am getting the bello
View Tutorial          By: Srinivasa Rao at 2009-04-02 05:17:20


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial