简体   繁体   English

即使存在.class文件也没有类错误

[英]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.

相关问题 即使存在主类,Java 也找不到主类 - Java cannot find main class even though main class is present ClassNotFoundException,即使包含该类的jar正确存在于类路径中 - ClassNotFoundException even though the jar containing the class is properly present in the classpath 错误告诉我运行类中的方法未定义,即使它是 - Error telling me that the method in my run class is undefined even though it is 即使 class 文件丢失或有错误,在第一次主动使用此 class 之前,预加载 class 不会报告错误? - Preloading class will not report an error until this class is first actively used even though the class file is missing or has errors? 找不到类,但是存在jar - Class Not Found Exception , though jar is present 找不到主类:即使Manifest指定了该类,Java也会出错 - Getting Could not find the main class: error in Java even though Manifest has the class specified 即使我在清单文件中定义了Main-class,也会出现“错误:找不到或加载类” - Getting “Error: Could not find or load class” even though I have the Main-class defined in a Manifest File 即使在上面的类中也找不到变量 - Variable not found even though it's in the class above 为什么没有调用A类中的toString? - Why is toString in class A evoked even though it is not called? 即使我注册也没有注册Kryonet类 - Kryonet class not registered even though I register
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM