2013-06-10 2 views
0

이것은 직렬 입력을 읽는 프로그램으로 모든 것이 잘 작동합니다. 성공적으로 컴파일하지만 아래 코드를 실행하려고하면 아래에 첨부 한 이미지의 오류가 표시됩니다.Java 런타임 오류

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

java -version
이에

+5

오류 이미지는 ** 읽을 ** 있습니다. 오류를 복사하여 질문에 붙여 넣으십시오. –

+0

jdk의 2 가지 버전이 설치되어 있습니까 ?? : 32 비트 및 64 비트 – codeMan

+2

'잘못된 경로'입니다. Java 경로를 확인하고 JAVA_HOME이 올바른지 확인하십시오. –

답변

0

단계 1. 명령 프롬프트 입력을 도와주십시오을 보여줍니다. 그렇지 않다면 자바를 설치해야합니다.

2 단계. JCreator (Java IDE)를 열고 Java 디렉토리의 올바른 경로를 설정하십시오.

단계 3. 제어판 -> 시스템 -> 고급 설정 -> 환경 변수에서 환경 변수를 엽니 다.

JAVA_DIRECTORY/bin

으로 클래스 경로를 설정/추가합니다. 4 단계 JAVA_DIRECTORY/jre/lib/ext 폴더에서 RXTX 및 JConnector와 같은 모든 .jar 파일을 복사합니다.

5 단계 컴퓨터를 껐다가 다시 켜십시오.