[英]No class error even though the .class files are present
Getting a Error: Could not find or load main class KnockKnockServer
, when running the below code using java KnockKnockServer 4444
, even though the class file KnockKnockServer.class is present in the directory. 出现
Error: Could not find or load main class KnockKnockServer
使用java KnockKnockServer 4444
运行以下代码时,即使目录中存在类文件KnockKnockServer.class ,也Error: Could not find or load main class KnockKnockServer
。
I was following this link to about learn the server-client working. 我正在关注此链接,以了解服务器-客户端的工作原理。
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();
}
}
If you don't pass the classpath on the commandline, it considers the CLASSPATH environment variable. 如果未在命令行上传递类路径,它将考虑CLASSPATH环境变量。 And only if that one is not set either, it considers the default one, being "."
并且只有在未设置任何一个的情况下,它才会将默认值视为“。”。
Therefore, either unset the CLASSPATH
environment variable or pass -cp .
因此,请取消设置
CLASSPATH
环境变量或通过-cp .
on the command line. 在命令行上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.