2

I'm trying to connect two machines together using sockets and i keep getting this error when i type java Client in the cmd. I set the path of the javac and i navigated to my project. I can compile it using javac Client.java but i can't run it using java. see the error in the attached picture.

enter image description here

i will attach the code of the client.

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);
    }

}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Mlak
  • 41
  • 6
  • the problem here is that the class `Client` is in a package `client` I guess. Please confirm. – AxelH Apr 17 '18 at 11:32
  • If I didnt put clinet package, it shows an error in compilation .. see the pic https://www.dropbox.com/s/hu50acqtn9cr9bh/AOS2.png?dl=0 – Mlak Apr 17 '18 at 14:57
  • Possible duplicate of [Java can't find main class](https://stackoverflow.com/questions/12096016/java-cant-find-main-class) – AxelH Apr 18 '18 at 09:05

1 Answers1

0

Take a look on this question/answer How do I run a Java program from the command line on Windows?

First of all you need to compile your .java file into .class file with "javac", and only then run it with "java"