我正在尝试通过Eclipse用Java编写客户端服务器应用程序。这是代码:
package edu.fcps.ab;
import java.io.*;
import java.net.*;
import java.util.*;
public class TestServer {
public static Vector<String> messages = new Vector<String>();
public static void main (String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter port number");
int port = scan.nextInt();
try {
ServerSocket serverSock = new ServerSocket(port);
Socket s = serverSock.accept();
int messageLength = 0;
PrintWriter output = new PrintWriter(s.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String inputLine;
while(true) {
if(messages.size() > messageLength) {
for(int i = messageLength; i < messages.size(); i++) {
System.out.println(messages.get(i));
}
messageLength = messages.size();
}
}
} catch(Exception e) {
System.out.println("An error occurred");
}
}
}
package edu.fcps.ab;
import java.io.*;
import java.net.*;
import java.util.*;
public class TestClient implements Runnable{
public static void main (String args[]) {
TestClient tc = new TestClient();
Thread t = new Thread(tc);
t.start();
}
@Override
public void run() {
Scanner s = new Scanner(System.in);
System.out.println("Enter port");
int port = s.nextInt();
String ip = "127.0.0.1";
try {
Socket sock = new Socket(ip, port);
System.out.println("Successfully connected");
System.out.println("Enter username");
String username = s.next();
PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String sent;
while(true) {
sent = stdin.readLine();
if(sent != null) {
TestServer.messages.add(username + ": " + sent);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
代码在Eclipse中可以正常编译,但是当我尝试通过命令行编译客户端时,出现此错误:
TestClient.java:31: error: package TestServer does not exist
TestServer.messages.add(username + ": " + sent);
服务器和客户端文件在同一程序包中。为什么会发生此错误,我该如何解决?