简体   繁体   English

Java运行时错误

[英]Java Runtime error

This is my program for reading a serial input , everything works fine . 这是我读取串行输入的程序,一切正常。 Compiles successfully but when I try to execute the below code , it will show an error the image of which I have attached below. 编译成功,但是当我尝试执行以下代码时,它将显示错误,并在下面附加了该图像。

import java.io.*;
import java.util.*;
import gnu.io.*;
import java.sql.*;


public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
static Connection con=null;
static String url = "jdbc:mysql://localhost:3306/track";
static String uid="root";
static String pwd="root";

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
    portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
             if (portId.getName().equals("COM1")) {
        //                if (portId.getName().equals("/dev/term/a")) {
                SimpleRead reader = new SimpleRead();
                    try
                    {

                        Class.forName("com.mysql.jdbc.Driver");
                        con = DriverManager.getConnection(url,uid,pwd);
                    }
                    catch(Exception s){
                        System.out.println(s);
                        }

        }
    }
}
}
public 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);}
    readThread = new Thread(this);
    readThread.start();
}

public void run() {
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {System.out.println(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:
        byte[] readBuffer = new byte[8];

        try {
                while (inputStream.available() > 0) {
                int numBytes = inputStream.read(readBuffer);
            }
            System.out.print(new String(readBuffer));
            } catch (IOException e) {System.out.println(e);}

        Statement st=null;
        try{
            st=con.createStatement();

           }
        catch(Exception ex){
            System.out.println(ex);
        }

        try{
            String idd = new String(readBuffer);

            if(idd.equals("05447646")||idd.equals("05447647")||idd.equals("05447649")||idd.equals("05447650")){

            String query = "INSERT INTO tablename1 (id) VALUES(?)";
            PreparedStatement pstm = con.prepareStatement(query);
            pstm.setString(1, idd);
            pstm.executeUpdate();
            pstm.close();}
            //st.executeUpdate("insert into asset(id) VALUES('"+idd+"'");
            //con.close();
            }
        catch(Exception ex){
            System.out.println(ex);
                    }

        }

}}

Error Image : 错误图片:

错误

Please Help 请帮忙

Step 1. Open command prompt and type in 步骤1.打开命令提示符并输入

java -version
This will show you where java is installed on your system. 这将向您显示Java在系统上的安装位置。 if it is not there then you need to install java 如果不存在,则需要安装java

Step 2. Open a JCreator (Your java IDE) then set the correct path to your java directory. 步骤2.打开一个JCreator(您的Java IDE),然后将正确的路径设置为您的java目录。

Step 3. Open environment Variables under Control Panel->System->Advance Settings-> Environment Variables. 步骤3.在“控制面板”->“系统”->“高级设置”->“环境变量”下打开环境变量。 and set/add a class path to 并设置/添加类路径
 JAVA_DIRECTORY/bin JAVA_DIRECTORY / bin 

Step 4 In your JAVA_DIRECTORY/jre/lib/ext folder copy all your .jar files like RXTX and JConnector and so on. 步骤4在您的JAVA_DIRECTORY / jre / lib / ext文件夹中,复制所有.jar文件,例如RXTX和JConnector等。

Step 5. Please turn the computer off and then on again 步骤5.请关闭计算机,然后再打开

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

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