简体   繁体   中英

how to fix the cannot find symbol compiler error

i am following a tutorial on how to make client/server programs in java (here it is: https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html ) and i got to the last part of the sockets tutorial where you make a client/server program where the server tells knock knock jokes and the client responds:

Server: "Knock knock!"

Client: "Who's there?"

Server: "Dexter."

Client: "Dexter who?"

Server: "Dexter halls with boughs of holly."

Client: "Groan."

I've copied the code for KnockKnockClient.java, KnockKnockServer.java, KnockKnockProtocol.java (I named these files Client.java, Server.java, Protocol.java) and now the last part for me to do is compile and run the code. I compiled the Protocol and Client code successfully, but when ever I try to compile the Server code I get an error looking like the first error

I have tried everything and I can't find a way to fix it. I know that the line of code that causes the error is trying to create an object using the KnockKnockProtocol.java file, but that's all that I could really figure out. I've checked the spelling, tried to compile the code from the parent folder of SocketsKnockKnockServer package and that did compile the code, but when I tried to run it from the parent folder I got a different error looking like the second error. And when I tried to run it from the package folder it gave me the same error as before.

//the code
package SocketsKnockKnockServer;

import java.net.*;
import java.io.*;

public class Server {

    public static void main(String[] args) throws IOException {

        if (args.length != 1) {
            System.err.println("Usage: Java Server.java <port number>");
            System.exit(1);
        }

        int portNumber = Integer.parseInt(args[0]);

        try (
            ServerSocket serverSocket = new ServerSocket(portNumber);
            Socket clientSocket = serverSocket.accept();
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        ) {

            String inputLine, outputLine;

            //intiate conversation with client
            Protocol kkp = new Protocol();
            outputLine = kkp.processInput(null);
            out.println(outputLine);

            while ((inputLine = in.readLine()) !=null) {
                outputLine = kkp.processInput(inputLine);
                out.println(outputLine);
                if (outputLine.equals("bye"))
                    break;
            }
        } catch (IOException e) {
            System.out.println("Exception caught when trying to listen to port " + portNumber + " or listening for a connection");
            System.out.println(e.getMessage());
        }

    }

}

Errors listed below

//the first error
Server.java:27: error: cannot find symbol
                        Protocol kkp = new Protocol();
                        ^
  symbol:   class Protocol
  location: class Server
Server.java:27: error: cannot find symbol
                        Protocol kkp = new Protocol();
                                           ^
  symbol:   class Protocol
  location: class Server
2 errors
error: compilation failed

//the second error
error: class found on application class path: SocketsKnockKnockServer.Server

Your java file is called KnockKnockProtocol.java yet the object you are creating is called Protocol. Either rename KnockKnockProtocol.java to Protocol.java or create an inner class within Server called Protocol. Also, it needs to be static.

I fixed the problem by transferring the files (Client.java, Server.java, Protocol.java) to the source folder instead of keeping them in a package.

Thanks to Jaywalker for suggesting the idea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM