0

Am trying to read COM port through javax.comm (WINDOWS 32) which implemented in Spirng MVC. Yes I got worked at development, if I deploy the WAR on Tomcat that throws error.

My Error Log:

    org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfig': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: void org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration.setConfigurers(java.util.Collection); nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.gvveg.sales.SalesController] for bean with name 'salesController' defined in file [C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\gvveg\WEB-INF\classes\com\gvveg\sales\SalesController.class]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: javax/comm/SerialPortEventListener
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.gvveg.sales.SalesController] for bean with name 'salesController' defined in file [C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\gvveg\WEB-INF\classes\com\gvveg\sales\SalesController.class]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: javax/comm/SerialPortEventListener
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: void org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration.setConfigurers(java.util.Collection); nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.gvveg.sales.SalesController] for bean with name 'salesController' defined in file [C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\gvveg\WEB-INF\classes\com\gvveg\sales\SalesController.class]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: javax/comm/SerialPortEventListener

This is my RequestMapping

@RequestMapping(value = "refreshWeight", method = RequestMethod.GET)
    public ResponseEntity<String> refreshWeight(){
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM7")) {
                     SimpleRead();
                }
            }
        }

        return new ResponseEntity<String> ( WEIGHT.trim(), HttpStatus.OK);
    }

This is my Simple Read method:

public void SimpleRead() {
        try {
            serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
        } catch (PortInUseException e) {System.out.println(e);}
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {System.out.println(e);}
    try {
            serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {System.out.println(e);}
        serialPort.notifyOnDataAvailable(true);
        try {
            serialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {System.out.println(e);}
        serialPort.close();
    }

Finally, seralEvent method

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:
        byte[] readBuffer = new byte[20];

        try {
            while (inputStream.available() > 0) {
                int numBytes = inputStream.read(readBuffer);
            }
            System.out.println(new String(readBuffer));
            WEIGHT = new String(readBuffer);
        } catch (IOException e) {System.out.println(e);}
        break;
    }   
}
dbreaux
  • 4,982
  • 1
  • 25
  • 64
Sankar
  • 687
  • 1
  • 13
  • 25

1 Answers1

-1

java.lang.NoClassDefFoundError: javax/comm/SerialPortEventListener

https://docs.oracle.com/javase/7/docs/api/java/lang/NoClassDefFoundError.html

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

Your Tomcat environment or WAR file are missing that class.

dbreaux
  • 4,982
  • 1
  • 25
  • 64
  • Yes you are right. But it throwing error on builtin class of SerialPortEventListener. From log : "java.lang.NoClassDefFoundError: javax/comm/SerialPortEventListener" – Sankar Jul 03 '18 at 15:55
  • Either it's not built-in (without some setting you have to enable), or it's in a classloader that the application can't see. I have no idea how Tomcat does such things. I'll add that tag to the question. – dbreaux Jul 03 '18 at 16:09