Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

How to install The Java Communications API in a Windows Environment

Posted by Circuit Negma on February 7, 2007

 

Fix to the previous Port on :: ” How to install The Java Communications API “.
————————————————————————————-

There is a trick to install the Java Communications API correctly on a Windows system Machine. The following files are the core of JAVA Communiccation API, and they are very important to have them installed on your system for a proper operation:

  • comm.jar
  • win32com.dll
  • javax.comm.properties

For the jdk (Java Developnment Kit) to recognize the serial ports on your machine, it is important to properly place these files in the right folders on your local machine :

%Java_HOME% = the location of your jdk directory.

To Find your JDK directory, Use the Following steps:

1. Click on Start
2. Click on Search
3. Click on For Files or Folders …
4. In the Left hand Side, Click on All Files and Folders
5. Type in jdk* in the textbox under All or part of the file name:
6. Click Search
7. Look for yellow icon that looks like a folder
8. Double Clikc on the folder to open the jdk folder

comm.jar should be placed in:

    %JAVA_HOME%/lib

    %JAVA_HOME%/jre/lib/ext

win32com.dll should be placed in:

    %JAVA_HOME%/bin

    %JAVA_HOME%/jre/bin

    %windir%System32

javax.comm.properties should be placed in:

    %JAVA_HOME%/lib

    %JAVA_HOME%/jre/lib

Download Link:

http://www.megaupload.com/?d=XSPXW2PY 

20 Responses to “How to install The Java Communications API in a Windows Environment”

  1. hamed said

    Thanks.

  2. aser hasan said

    I dont know what to say,But realy Very Very thank to you professional

  3. Billy said

    This was exactly what I needed, Thank-you.

  4. a said

    many thanx man

  5. pegah said

    wow it was great.
    thanks

  6. Five said

    *thumps up*
    thx!! =)

  7. satyam said

    i did they same but the program is throwing compile time exception
    its
    java.lang.noclassdiffounderror: javax/comm/commportidentifier

    i think my javax.comm is not installed properly plz suggest some thing.
    satyam

    • please verify the following:
      1. is java jdk is instsalled

      2. replace the following files in the following directories:
      [-win32com.dll-] should be copied to the following directories:
      %JAVA_HOME%/bin == C:\Program Files\Java\jdk1.5.0_10\bin
      %JAVA_HOME%/jre/bin == C:\Program Files\Java\jdk1.5.0_10\jre\bin
      %windir%/System32 == C:\windows\system32
      Here is how to copy the file to the above directories:
      a. click start
      b. click run
      c. type in one of the above directories in the textfield and hit ENTER, this will pop-up a window directed to the specified directory. if this does not work, this means that your windows system environmental variables for java is missing.
      d. copy the file to that directory.

      [-comm.jar-] should be copied to the following directories:
      %JAVA_HOME%/lib == C:\Program Files\Java\jdk1.5.0_10\lib
      %JAVA_HOME%/jre/lib/ext == C:\Program Files\Java\jdk1.5.0_10\jre\lib\ext

      [-javax.comm.properties-] should be copied to the following directories:
      %JAVA_HOME%/lib == C:\Program Files\Java\jdk1.5.0_10\lib
      %JAVA_HOME%/jre/lib == C:\Program Files\Java\jdk1.5.0_10\jre\lib

      3. you need to include comm.jar in your project settings or properties
      this is an example, I use netbeans for my java developments:

      Here is a sample code:

      //Sun’s serial port driver
      import javax.comm.*;
      import java.io.*;
      import java.util.*;

      public class com implements Runnable, SerialPortEventListener {

      static CommPortIdentifier portId1;
      static CommPortIdentifier portId2;

      InputStream inputStream;
      OutputStream outputStream;
      SerialPort serialPort1, serialPort2;
      Thread readThread;
      protected String divertCode = “10″;
      static String TimeStamp;

      public com()
      {
      try
      {
      TimeStamp = new java.util.Date().toString();
      serialPort1 = (SerialPort) portId1.open(”portId1(” + portId1.getName().toString() + “)”, 1000);
      System.out.println(TimeStamp + “: ” + portId1.getName() + ” opened for scanner input”);

      }
      catch (PortInUseException e)
      {}

      try
      {
      inputStream = serialPort1.getInputStream();
      }
      catch (IOException e)
      {}

      try
      {
      serialPort1.addEventListener(this);
      }
      catch (TooManyListenersException e)
      {}

      serialPort1.notifyOnDataAvailable(true);

      try
      {
      serialPort1.setSerialPortParams(38400,
      SerialPort.DATABITS_8,
      SerialPort.STOPBITS_1,
      SerialPort.PARITY_NONE);

      serialPort1.setDTR(false);
      serialPort1.setRTS(true);

      }
      catch (UnsupportedCommOperationException e)
      {}

      readThread = new Thread(this);
      readThread.start();
      }

      public void run()
      {
      try
      {
      Thread.sleep(100);
      }
      catch (InterruptedException e) {}
      }

      public static void main(String[] args) {

      try
      {
      // Scan for available ports on local host machine.
      CommPortIdentifier cpi = null;
      for (Enumeration e = CommPortIdentifier.getPortIdentifiers() ; e.hasMoreElements() ;)
      {
      CommPortIdentifier tmp = (CommPortIdentifier)e.nextElement();
      System.out.println(”COM = ” + tmp.getName().toString() + ” mem = ” + tmp.toString());
      System.out.println(”e = ” + e.toString());

      if (tmp.getName().equals(”COM1″))
      {
      cpi = tmp;
      System.out.println(”this is COM1: string”);
      break;
      }
      }

      // Defien a port manually
      portId1 = CommPortIdentifier.getPortIdentifier(”COM1″);

      // Create new COM thread and start listening to the port.
      com reader = new com();

      }

      catch
      (Exception e) {
      TimeStamp = new java.util.Date().toString();
      System.out.println(TimeStamp + portId1.getName().toString() + portId1);
      System.out.println(TimeStamp + “: msg1 – ” + e);
      }
      }

      public void serialEvent(SerialPortEvent event) {
      switch(event.getEventType()) {
      case SerialPortEvent.BI:
      case SerialPortEvent.OE:
      case SerialPortEvent.FE:
      case SerialPortEvent.PE:
      case SerialPortEvent.CD:
      case SerialPortEvent.CTS:
      case SerialPortEvent.DSR:
      case SerialPortEvent.RI:
      case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
      break;
      case SerialPortEvent.DATA_AVAILABLE:
      StringBuffer readBuffer = new StringBuffer();
      int c;
      try
      {
      c=inputStream.read();
      while ( (c != 1) || (c != 2) || (c != 3) || (c != 4))
      {
      //readBuffer.append((char) c);
      System.out.print(c);
      }

      String scannedInput = readBuffer.toString();
      TimeStamp = new java.util.Date().toString();
      System.out.println(TimeStamp + “: scanned input received:” + scannedInput);

      inputStream.close();

      readBuffer = new StringBuffer(0);
      /* if(scannedInput.substring(0,1).equals(”F”)){
      outputStream = serialPort1.getOutputStream();
      outputStream.write(divertCode.getBytes());
      System.out.println(TimeStamp + “: diverter fired”);
      outputStream.close();
      }
      else
      {
      System.out.println(TimeStamp + “: diverter not diverted”);
      }
      */
      } catch (IOException e) {}

      break;
      }
      }
      }

  8. Vlatko said

    Hi, I've done the previous but it is not working. import javax.comm.*; can't pass.

    I have no idea why?

    Vlatko

  9. Vlatko said


    actually, I found it. In some of the previous comments was said that you should place files in ...jdk... folder. But when you open a project, check for the path of the already added libraries(below the package folder in solution explorer).

    In my project that folder was ...jre... instead of ...jdk...

    greetings

  10. Vlatko said

    I was happy when found your explanation. I still am. :)

    Thank you.

  11. Vlatko said


    And can someone tell me how to do the same under Unix platform?

    Tnaks,
    Vlatko

  12. Vlatko said


    I've tried and worked that with rxtxComm.

    http://www.captain.at/howto-java-serial-port-javax-comm-rxtx.php

    I am trying to do it with comm.jar(your win version but for linux).
    You know, that gnu.io.* got the same functions as your javax.comm. Is it posible to find
    it for linux. Just that properties.
    In properties, there is a win32 and so...
    But, it is not so important for me to work with javax.comm, I am a biginner. :)

    I am a recent linux user. You know, it is not so bad and scary.

    Greetings,

    • Linux is not so hard to learn. as long as you understand the system ext., system folders and some basic commands, you will be surprised how easy it is.

      I used Linux long time ago for my school. but I stopped using it since I finished schools and turned back to windows because most of my design softwares run on windows only.

      As much as I would like to help every one with the Comm api, I am no longer have a linux machine for experiments.

      If you do find the solution would you be so kind to post it here so that every one could learn from your help.

      thank you so much Vlatko

  13. smn_007 said

    hai…
    I have a java program to poll a sensor device every 2 min…. The program uses java comm to read and write data to it. I have java servlet program that has to access the serial port at the same time as the polling program to read/write data from it….. I.It works individually….it is not possible to access the serial port at the same time….. Can any time slice help….. or anything which could get it done
    plz can anyone help with it….
    the project has to completed within a week

    Thanks in advance
    smn

  14. smn_007 said

    hai…

    Can 2 threads access the serial port through wait and notify…. can anyone help me with the code…. using java comm package

    smn

  15. Shaswat said

    Thnks…..

  16. Jecelyn said

    Cool man, u really help me out!! Thanks lot!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>