【问题标题】:cmd "class not found"cmd“找不到类”
【发布时间】:2018-09-27 07:48:22
【问题描述】:

我正在尝试使用套接字将两台机器连接在一起,当我在 cmd 中键入 java Client 时,我不断收到此错误。我设置了javac的路径 我导航到我的项目。我可以使用 javac Client.java 编译它,但我不能使用 java 运行它。 请参阅附图中的错误。

我会附上客户端的代码。

public class Client {

    ObjectInputStream Sinput;       // to read the socket
    ObjectOutputStream Soutput; // towrite on the socket
    Socket socket;

    // Constructor connection receiving a socket number
    Client(int port) {
        // we use "localhost" as host name, the server is on the same machine
        // but you can put the "real" server name or IP address
        try {
            socket = new Socket("192.168.43.115", port);
        }
        catch(Exception e) {
            System.out.println("Error connectiong to server:" + e);
            return;
        }
        System.out.println("Connection accepted " +
                socket.getInetAddress() + ":" +
                socket.getPort() + "\n");

        /* Creating both Data Streams */
        try
        {
            Sinput  = new ObjectInputStream(socket.getInputStream());
            Soutput = new ObjectOutputStream(socket.getOutputStream());
        }
        catch (IOException e) {
            System.out.println("Exception creating new Input/output Streams: " + e);
            return;
        }
        // my connection is established
        String test = new String ();
        test = "What is the date & time now ??";
        // send the question (String) to the server
        System.out.println("Client sending \"" + test + "\" to serveur\n");
        try {
            Soutput.writeObject(test);
            Soutput.flush();
        }
        catch(IOException e) {
            System.out.println("Error writting to the socket: " + e);
            return;
        }
        // read back the answer from the server
        String response;
        try {
            response = (String) Sinput.readObject();
            System.out.println("Read back from server: " + response);
        }
        catch(Exception e) {
            System.out.println("Problem reading back from server: " + e);
        }

        try{
            Sinput.close();
            Soutput.close();
        }
        catch(Exception e) {}

        }
    public static void main(String[] args) {
       new Client(1500);
    }

}

【问题讨论】:

标签: java sockets cmd operating-system


【解决方案1】:

看看这个问题/答案How do I run a Java program from the command line on Windows?

首先你需要用“javac”把你的.java文件编译成.class文件,然后用“java”运行它

【讨论】:

    猜你喜欢
    • 2021-02-14
    • 2021-10-28
    • 2017-10-05
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 2017-12-20
    • 1970-01-01
    相关资源
    最近更新 更多