简体   繁体   English

线程“主”java.lang.NoClassDefFoundError 中的异常:gnu/io/SerialPortEventListener

[英]Exception in thread “main” java.lang.NoClassDefFoundError: gnu/io/SerialPortEventListener

I am new to coding and i want to connect my pi to Schneider pm 1200 to get values of holding registers.我是编码新手,我想将我的 pi 连接到 Schneider pm 1200 以获取保持寄存器的值。 and when i executed it, i got many errors related to exception.当我执行它时,我遇到了许多与异常相关的错误。

How to write register?怎么写寄存器? I am trying to use writeSingleRegister method,but I cant's get it's register type error.我正在尝试使用 writeSingleRegister 方法,但我无法得到它的寄存器类型错误。

I am using j2mod jar file我正在使用j2mod jar 文件

This is the code这是代码

import com.ghgande.j2mod.modbus.facade.ModbusSerialMaster;
import com.ghgande.j2mod.modbus.Modbus;
import com.ghgande.j2mod.modbus.ModbusException;
import com.ghgande.j2mod.modbus.procimg.Register;
import com.ghgande.j2mod.modbus.util.*;

public class ModbusMaster
{

/**
 * @param args
 */
    public static void main(String[] args) 
    {
        /* The important instances of the classes mentioned before */
        ModbusSerialMaster serialMaster = null; // the connection
        
        /* Variables for storying the parameters */
        String portname = "/dev/ttyUSB0"; // the name of the serial port to be used
        int unitID = 1 ; // the unit identifier we will be talking to
        int startingRegister = 100001; // the reference, where to start reading from
        int registerCount = 0; // the count of the input registers to read
        Register[] slaveResponse = new Register[registerCount];
    
        try 
        {
            /* Setup the serial parameters */
            SerialParameters parameters = new SerialParameters();
            parameters.setPortName(portname);
            parameters.setBaudRate(9600);
            parameters.setDatabits(8);
            parameters.setParity("None");
            parameters.setStopbits(1);
            parameters.setEncoding(Modbus.SERIAL_ENCODING_RTU);
            parameters.setEcho(false);
    
            /* Open the connection */
            serialMaster = new ModbusSerialMaster(parameters);
            serialMaster.connect();
    
        } 
        
        catch (Exception exception) 
        {
            exception.printStackTrace();
        }
    
        /* Read the first four holding registers */
        try 
        {
            slaveResponse = serialMaster.readMultipleRegisters(unitID, startingRegister, registerCount);
            for (int i = 0; i < slaveResponse.length; i++) 
            {
                System.out.println("reg" + i + " = " + slaveResponse[i]);
            }
        } 
        
        catch (ModbusException e) 
        {
            e.printStackTrace();
        }
        /* Close the connection */
        serialMaster.disconnect();
    
    }
}

this is the output这是 output

Exception in thread "main" java.lang.NoClassDefFoundError: gnu/io/SerialPortEventListener
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.ghgande.j2mod.modbus.facade.ModbusSerialMaster.<init>(ModbusSerialMaster.java:78)
    at ModbusMaster.main(ModbusMaster.java:38)
Caused by: java.lang.ClassNotFoundException: gnu.io.SerialPortEventListener
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 14 more

Your answer will be helpful for me.你的回答会对我有所帮助。

At runtime, the code executed for a Java program starts out in "class files", stored by the compiler in files with a ".class" extension.在运行时,为 Java 程序执行的代码从“类文件”开始,由编译器存储在扩展名为“.class”的文件中。 These files are often packaged in groups in "jar files", and the Java runtime can find the classes in the jar files while it's running.这些文件通常分组打包在“jar 文件”中,Java 运行时可以在运行时找到 jar 文件中的类。

Your error indicates that the runtime cannot find the class indicated.您的错误表明运行时找不到指示的 class。 The java.io.SerialPortEventListener class appears to be available in the jar file rxtx-2.1.7.jar. The java.io.SerialPortEventListener class appears to be available in the jar file rxtx-2.1.7.jar. You can download that (search for its reliable source) and copy it to the target machine.您可以下载它(搜索其可靠来源)并将其复制到目标机器。 You'll then need to read up on what a classpath is;然后,您需要阅读什么是类路径; you can set a classpath in an environment variable or with a parameter passed to the "java" command at runtime.您可以在环境变量中设置类路径,也可以在运行时将参数传递给“java”命令。 Either way, the runtime can find the class in that jar file when you run your program.无论哪种方式,运行程序时,运行时都可以在 jar 文件中找到 class。

I've touched on a handful of concepts here;我在这里触及了一些概念; am sorry it isn't a real simple do 1-2-3 kind of answer, but you'll need to understand jar files and the classpath as you code more.很抱歉,这不是一个真正简单的 1-2-3 类答案,但是当您编写更多代码时,您需要了解 jar 文件和类路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM