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 

34 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!

  17. Caesar said

    hi all

    I’m seeking this guide to properly install the javax.comm extension.
    I don’t know well how it works…
    I really need to map COM ports using the JAVA ICA Client through Adito Open VLS, by the time i ‘ve “drive and printer mapping” working great but unfortunately in a citrix troubleshooting guide says that COM port MAPPING is not supported by de JICA Client Applet. =(

    I really need it working cuz my App works with Epson Fiscal Printers that only can communicate through that annoying COM port

    BTW Thanks a lot to all of you. My apollogies, my english sucks…

    César

  18. smn_07 said

    Hey the how to install java comm api can be found on this page which was posted earlier in this website…

    http://circuitnegma.wordpress.com/2007/02/07/how-to-install-the-java-communications-api-in-a-windows-environment/

    this will definetly help u out

  19. Dan said

    Hi,

    I installed as per your instruction. But it is not working for me. What OS you are using?
    I am on Windows Vista. Is it possible to access direct IO from the user-mode? as per my knowledge, from windows XP onwards we need a SYS file to access the IO directly. Can you help me out?

    Regards,
    Dan

    • Hi Dan,

      I was using Windows XP. Then I moved to xp once I was done with my project.

      Sorry, I do not know to to get the API working on XP.

      However, I knew that to obtain a COM (port) you need to obtain an ownership of the targeted port. Under XP this is very trivial as XP will let you take over the ownership, where as the Vista OS would not let to do until you identify your self as the administrator to gain ownership of that port, thu I could be wrong.

      Also, I do not know if this API is compatible with Vista.

      try thin one : GiovynetSerialPort1.3

      Try to sear google such keywords as : java communication api for vista

      hopefully this will help you

  20. DINESH Rajapaksha said

    Thnx a Lot…!! now its wokring !
    this will help a lot in my Final year project :) )

    thnx again

  21. tuansoibk said

    thanks a lot, it’s really helpful!

  22. jiggy360 said

    Im not able to run sample program in windows…
    Exception in thread “main” java.lang.NoClassDefFoundError: javax/comm/CommPortIdentifier

    I have installed everything exactly stated above.. still im getting this error
    please help!!!

  23. sundarrajan said

    hi sir

    i need a help urgent … help me for my project

    I just need to list the comm ports available in windows. i have successfully the javax.comm package as you have mentioned earlier in this blog.

    I m having this 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);
    }
    }
    }

    The above program is compiling well and good in my system.. but when i try to run it its shows me the error as

    Caught java.lang.ClassNotFoundException: com.sun.comm.Win32Driver while loading
    driver com.sun.comm.Win32Driver
    Error loading SolarisSerial: java.lang.UnsatisfiedLinkError: no SolarisSerialPar
    allel in java.library.path
    Caught java.lang.UnsatisfiedLinkError: com.sun.comm.SolarisDriver.readRegistrySe
    rial(Ljava/util/Vector;Ljava/lang/String;)I while loading driver com.sun.comm.So
    larisDriver

    I have done all the things clear.. but i m ambiguous where the problem is .

    Thanks in advance

    Please try to run this program in your system and then reply for me

    • I love to help out, but there is one small problem. My new computer does not have any COM ports, However, I am going to try to compile you code. Also, I am going to look for my old code that did utilize COM ports on my old machine and post it here.

    • Ok, so I compiled you code on my machine, and as you said it does compile, but I cannot run it on my machine because it is Vista 64-bit machine.

      Netbeans IDE shoes the following error every time I try to run the code:

       run:
      Error loading win32com: java.lang.UnsatisfiedLinkError: C:\Program Files\Java\jdk1.6.0_17\jre\bin\win32com.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
      BUILD SUCCESSFUL (total time: 0 seconds)
      

      I am going to look for the old codes and post them here.

  24. Vijay said

    Dear Circuit Negma,

    Thank you very very much. It is what exactly I was looking for and I did not find any exact solution in this clear way till I visited your post here.

    Thanks a lot,
    Vijay

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>