• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 802
  • Last Modified:

Listing Comm ports on WinXP SP2

Hi,

I have the following code from the book Java I/O:

import javax.comm.*;
public class NamedPortLister {
  public static void main(String[] args) {
    // List serial (COM) ports.
    try {
      int portNumber = 1;
      while (true) {
        CommPortIdentifier.getPortIdentifier("COM" + portNumber);
        System.out.println("COM" + portNumber);
        portNumber++;
      }

    }
    catch (NoSuchPortException ex) {
      // Break out of loop.
    }
    // List parallel (LPT) ports.
    try {
      int portNumber = 1;
      while (true) {
        CommPortIdentifier.getPortIdentifier("LPT" + portNumber);
        System.out.println("LPT" + portNumber);
        portNumber++;
      }
    }
    catch (NoSuchPortException ex) {
      // Break out of loop.
    }
  }
}

I have tried Netbeans & the command line. Both compile and *seem* to run, however there is no output??
Running WinXp SP2
Java Comm 2.0
JDK 1.5.07

Cheers,
Craig
0
cbumpste
Asked:
cbumpste
  • 5
  • 3
  • 2
  • +1
1 Solution
 
Tommy BraasCommented:
You are probably getting an exception, which you are ignoring. Ignoring as VERY BAD practice!

Add a:
ex.printStackTrace();
to each catch to see what is going on
0
 
Tommy BraasCommented:
'as' in the above should be 'is'... :-)

Such that:
Ignoring exceptions is VERY BAD practice!
0
 
girionisCommented:
Try to code the catch block, it might be going in there, i.e. not finding any port. Change this

>  catch (NoSuchPortException ex) {
>       // Break out of loop.
>     }

to

catch (NoSuchPortException ex) {
     System.out.println("Exception: " + ex);
    }

and see if you get any error messages.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
objectsCommented:
make sure you corectly installed the javacomm dll and properties file as per the install document.
0
 
girionisCommented:
And make sure the javax.comm.properties is in the correct place (<jdk>/lib), otherwise have a look in the installation instructions for alternative locations.
0
 
girionisCommented:
Bah... objects was faster.
0
 
cbumpsteAuthor Commented:
Thanks guys,

I added the exception that you suggested. I got the following:


javax.comm.NoSuchPortException
        at javax.comm.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:105)
        at NamedPortLister.main(NamedPortLister.java:8)
Exception: javax.comm.NoSuchPortException
Exception: javax.comm.NoSuchPortException

No such port? I have a physical port on my laptop.
I have Administrator rights on the box.

Cheers,
Craig
0
 
girionisCommented:
Try the following:

1) Copy the javax.comm.properties into your $JAVA_HOME/jre/lib folder
2) copy the win32com.dll into your $JAVA_HOME/bin

and try again.
0
 
cbumpsteAuthor Commented:
girionis,

The jre directory under the jdk directory was missing those files. Bugger!

Thanks again everyone!!!
0
 
girionisCommented:
:)
0
 
cbumpsteAuthor Commented:
Guys,

Sorry that it appears that only a single person can be assigned points.

The tips on the printing exceptions were good as well.

Cheers,
Craig
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now