編譯報錯+解決方法:錯誤: 找不到符號
阿新 • • 發佈:2019-02-19
【0】README
1)本文主要解決“錯誤,找不到符號”
【1】 console error info
1)error :
2)reasons of errors: 參考一下程式碼,ServerPlainTest 依賴於 ReceiverHandler.java and SendHandler.java ;而 ReceiverHandler.java and SendHandler.java 定義在 MessageHandler.java ,它們屬於包內可見性,包外不可見; 由編譯報錯的結果來看,當javac 編譯器去編譯ServerPlainTest.java的時候,它 不會去編譯 包可見性的類,所以就不會產生 相應的 .class 檔案,所以報錯;E:\bench-cluster\mybatis_in_action_eclipse\xmpp\src>javac com/communication/qq/ServerPlainTest.java com\communication\qq\ServerPlainTest.java:14: 錯誤: 找不到符號 new Thread(new ReceiveHandler(s, Role.SERVER)).start(); ^ 符號: 類 ReceiveHandler 位置: 類 ServerPlainTest com\communication\qq\ServerPlainTest.java:15: 錯誤: 找不到符號 new Thread(new SendHandler(s, Role.SERVER)).start(); ^ 符號: 類 SendHandler 位置: 類 ServerPlainTest 2 個錯誤
public class ServerPlainTest { // 包內包外可見 public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8189); System.out.println("the server has startuped, waiting for connections."); while (true) { // accept multiple clients connection request. Socket s = ss.accept(); System.out.println("a client has connected successfully."); new Thread(new ReceiveHandler(s, Role.SERVER)).start(); new Thread(new SendHandler(s, Role.SERVER)).start(); } } catch (Exception e) { System.out.println(e); } } }
【2】solution:在編譯 ServerPlainTest.java 之前,編譯 MessageHandler.java, 這樣會附帶編譯 ReceiverHandler.java and SendHandler.java ;public class MessageHandler { // 包內包外可見. } // receive msg. class ReceiveHandler implements Runnable { // 包內可見性. private Socket socket; private Role role; public ReceiveHandler(Socket socket, Role role) { this.socket = socket; this.role = role; } public void run() { try { System.out.println(role + " executes ReceiveHandler.run method."); InputStream is = socket.getInputStream(); Scanner in = new Scanner(is); // echo client input boolean done = false; while (!done && in.hasNextLine()) { String line = in.nextLine(); System.out.println("from "+ role + ": " + line); if (line.trim().equalsIgnoreCase("bye")) { done = true; } } // close relative instance. in.close(); } catch (IOException e) { e.printStackTrace(); } } } // send msg. class SendHandler implements Runnable { // 包內可見性. private Socket socket; private Role role; public SendHandler(Socket socket, Role role) { this.socket = socket; this.role = role; } public void run() { PrintWriter pw = null; try { System.out.println(role + " executes SendHandler.run method."); Scanner s = new Scanner(System.in); OutputStream os = socket.getOutputStream(); pw = new PrintWriter(os, true); // autoFlush=true. if (role.equals(Role.SERVER)) { pw.println("from server: you've connected with the server."); } while (s.hasNext()) { String line = s.nextLine(); pw.println(line); } pw.println("connection closed successfully. bye!"); } catch (Exception e) { e.printStackTrace(); } } }
E:\bench-cluster\mybatis_in_action_eclipse\xmpp\src>javac com/communication/qq/ServerPlainTest.java com/communication/qq/MessageHandler.java