编译报错+解决方法:错误: 找不到符号

【0】README
1)本文主要解决“错误,找不到符号”

【1】 console error info
1)error : 
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 个错误
2)reasons of errors:  参考一下代码,ServerPlainTest 依赖于 ReceiverHandler.java  and SendHandler.java ;而 ReceiverHandler.java and SendHandler.java 定义在 MessageHandler.java ,它们属于包内可见性,包外不可见; 由编译报错的结果来看,当javac 编译器去编译ServerPlainTest.java的时候,它 不会去编译 包可见性的类,所以就不会产生 相应的 .class 文件,所以报错;
编译报错+解决方法:错误: 找不到符号_第1张图片
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);
		}
	}
}
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();
		}  
	}
}
【2】solution:在编译 ServerPlainTest.java 之前,编译 MessageHandler.java, 这样会附带编译 ReceiverHandler.java and SendHandler.java ;
E:\bench-cluster\mybatis_in_action_eclipse\xmpp\src>javac com/communication/qq/ServerPlainTest.java com/communication/qq/MessageHandler.java

你可能感兴趣的:(编译报错+解决方法:错误: 找不到符号)