[英]No class error even though the .class files are present
is present in the directory. 出现Error: Could not find or load main class KnockKnockServer
使用java KnockKnockServer 4444
运行以下代码时,即使目录中存在类文件 ,也Error: Could not find or load main class KnockKnockServer
。
我正在关注此链接,以了解服务器-客户端的工作原理。
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();
}
}
如果未在命令行上传递类路径,它将考虑CLASSPATH环境变量。 并且只有在未设置任何一个的情况下,它才会将默认值视为“。”。
因此,请取消设置CLASSPATH
环境变量或通过-cp .
在命令行上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.