使用Error: Could not find or load main class KnockKnockServer
运行以下代码时获取java KnockKnockServer 4444
,即使目录中存在类文件 KnockKnockServer.class 。
我正在关注this link以了解服务器客户端的工作情况。
import java.net.*;
import java.io.*;
public class KnockKnockServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
答案 0 :(得分:0)
如果您未在命令行上传递类路径,则会考虑CLASSPATH环境变量。并且只有当它没有被设置时,它才会考虑默认值,即"。"
因此,要么取消设置CLASSPATH
环境变量,要么在命令行上传递-cp .
。