2

I've got two files to compile: Client.java and ClientHandler.java.

I have to compile and run them in a script. This is the compiling script I wrote so far:

del src\pack\*.class
set path="C:\Program Files\Java\jdk1.8.0_144\bin"
javac ClientHandler.java
javac Client.java
pause

The Client occasionally creates ClientHandler objects, and on that line the command line throws an error.

C:\Users\maksg\Desktop\abc>set path="C:\Program Files\Java\jdk1.8.0_144\bin"
C:\Users\maksg\Desktop\abc>javac ClientHandler.java
C:\Users\maksg\Desktop\abc>javac Client.java
Client.java:44: error: cannot find symbol
                                                        Thread ch = new ClientHandler(s, din, dout, gracze);
                                                                        ^
  symbol: class ClientHandler
1 error

C:\Users\maksg\Desktop\abc>pause

This is my script for running the program:

set path="C:\Program Files\Java\jre1.8.0_144\bin"
start java Client gracz1 7000 7000 127.0.0.1
start java Client gracz2 6000 7000 127.0.0.1
start java Client gracz3 8000 6000 127.0.0.1
start java Client gracz4 9000 7000 127.0.0.1

However, I can't even check if the running script is correct because I wasn't able to compile those 2 files.

Here are the source codes for both classes: Client

package a;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;


public class Client extends Thread{

    static String nazwa;
    static int portMoj;
    static int portInnegoKlienta;
    static String ip;
    static HashMap<Integer, String> gracze;
    static String liczbaPrzeciwnika;
    static int portPrzeciwnika;
    static Thread ch;

    public Client(String[] args) {
        gracze = new HashMap<>();
        nazwa = args[0];
        portMoj = Integer.parseInt(args[1]);
        portInnegoKlienta = Integer.parseInt(args[2]);
        ip = args[3];
    }

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

        Thread t = new Thread(new Runnable() {          
            @Override
            public void run() {
                try {
                ServerSocket serverSocket = new ServerSocket(portMoj);
                    while(true) {
                            Socket s = serverSocket.accept();
                            DataInputStream din = new DataInputStream(s.getInputStream());
                            DataOutputStream dout = new DataOutputStream(s.getOutputStream());
                            Thread ch = new ClientHandler(s, din, dout, gracze);
                            ch.start();                         
                    }
                }catch(Exception ex) {
                    System.out.println(ex);
                    ex.printStackTrace();
                }

            }
        });
        t.start();

        try {
            System.out.println("Uruchomiony Klient " + nazwa);          
            System.out.println("Moj port: " + args[1]);
            System.out.println("Port klienta wpr.: " + args[2]);

            gracze.put(Integer.parseInt(args[1]), nazwa);

            Socket socket = null;
            DataInputStream din = null;
            DataOutputStream dout = null;

            boolean skanuj=true;
            while(skanuj){
                try{
                    socket = new Socket(ip, portInnegoKlienta);

                    System.out.println("Nawiazywanie polaczenia z: " + portInnegoKlienta);

                    din = new DataInputStream(socket.getInputStream());
                    dout = new DataOutputStream(socket.getOutputStream());

                    skanuj=false;
                }
                catch(Exception e){
                    System.out.println("Polaczenie nieudane, ponawiam polaczenie");
                    try{
                        Thread.sleep(2000);//co 2 sek
                    }
                    catch(InterruptedException ie){
                        ie.printStackTrace();
                    }
                }
            }

            System.out.println("Wysylam join do...: " + portInnegoKlienta);

            dout.writeUTF("join " + nazwa + " " + portMoj+"");
            dout.flush();


            gracze.put(portMoj, nazwa);

            socket = new Socket(ip, portInnegoKlienta);
            System.out.println("Nawiazywanie polaczenia z: " + portInnegoKlienta);
            din = new DataInputStream(socket.getInputStream());
            dout = new DataOutputStream(socket.getOutputStream());
            Thread.sleep(1000);

            String liczbaWylosowana = ((int)(Math.random()*10))+"";
            String mojPortString = portMoj+"";
            dout.writeUTF("game " + liczbaWylosowana + " " + mojPortString);
            dout.flush();

            socket = new Socket(ip, portInnegoKlienta);
            System.out.println("Nawiazywanie polaczenia z: " + portInnegoKlienta);
            din = new DataInputStream(socket.getInputStream());
            dout = new DataOutputStream(socket.getOutputStream());
            Thread.sleep(1000);

        } catch(Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }
}

And ClientHandler:

package a;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

class ClientHandler extends Thread{ 

    DataInputStream din; 
    DataOutputStream dout; 
    Socket socket;
    HashMap<Integer, String> gracze;

    public ClientHandler(Socket socket, DataInputStream din, DataOutputStream dout, HashMap<Integer, String> gracze){ 
        this.socket = socket; 
        this.din = din; 
        this.dout = dout;
        this.gracze = gracze;

    } 

    public void run() {
        String[] komunikaty;
        String msgin; 

            try {
                msgin = din.readUTF();
                komunikaty = msgin.split(" ");

                if(komunikaty[0].equals("join")){
                    System.out.println("Uzyskalem komende join");
                    String nazwaPartnera = komunikaty[1];
                    String portPartnera = komunikaty[2];
                    System.out.println("Polaczyl sie ze mną: " + nazwaPartnera
                             + ", na porcie: " + portPartnera);
                    gracze.put(Integer.parseInt(portPartnera), nazwaPartnera);

                    String wszyscyGracze = "aktywni ";
                    Set<Integer> set = gracze.keySet();
                    for(int i: set) {
                        wszyscyGracze += i + " " + gracze.get(i) + " ";
                    }
                    for(int i : set) {
                        Socket s = new Socket(socket.getInetAddress(), i);
                        dout = new DataOutputStream(s.getOutputStream());
                        dout.writeUTF(wszyscyGracze);
                        dout.flush();
                        s.close();
                    }
                }
                if(komunikaty[0].equals("aktywni")) {
                    System.out.println("Uzyskalem komende aktywni");
                    System.out.println("Lista aktywnych graczy: ");
                    gracze.clear();                 
                    for(int i = 1; i < komunikaty.length-1; i+=2) {
                        gracze.put(Integer.parseInt(komunikaty[i]), komunikaty[i+1]);
                    }
                    Set<Integer> set = gracze.keySet();
                    for(int i : set) {
                        System.out.println(i + " " + gracze.get(i));
                    }
                }
                if(komunikaty[0].equals("game")) {
                    System.out.println("Uzyskalem komende game");
                    String liczbaGraczaWyzywajacego = komunikaty[1];
                    int portGraczaWyzywajacego = Integer.parseInt(komunikaty[2]);
                    String nazwaGraczaWyzywajacego = gracze.get(portGraczaWyzywajacego);
                    Set<Integer> set = gracze.keySet();
                    ArrayList<String> rezultatyMeczy = new ArrayList<>();

                    for(int i : set) {                      
                        Socket s = new Socket(socket.getInetAddress(), i);
                        dout = new DataOutputStream(s.getOutputStream());
                        if(!(i == portGraczaWyzywajacego)) {
                            int liczbaPrzeciwnika = (int)(Math.random()*10);
                            if(((Integer.parseInt(liczbaGraczaWyzywajacego)+liczbaPrzeciwnika)%2) == 0) {
                                rezultatyMeczy.add("Wygralem z " + gracze.get(i));
                                dout.writeUTF("challenged " + portGraczaWyzywajacego + " " + nazwaGraczaWyzywajacego + " 0");
                                dout.flush();
                            } else {
                                rezultatyMeczy.add("Przegralem z " + gracze.get(i));
                                dout.writeUTF("challenged " + portGraczaWyzywajacego + " " + nazwaGraczaWyzywajacego + " 1");
                                dout.flush();
                            }
                        }
                        s.close();
                        dout.close();
                    }
                    String wiadomoscZwrotnaDoWyzywajacego = "results ";
                    for(int i = 0; i < rezultatyMeczy.size(); ++i) {
                        wiadomoscZwrotnaDoWyzywajacego += rezultatyMeczy.get(i) + " ";
                    }
                    Socket zwrotny = new Socket(socket.getInetAddress(), portGraczaWyzywajacego);
                    dout = new DataOutputStream(zwrotny.getOutputStream());
                    dout.writeUTF(wiadomoscZwrotnaDoWyzywajacego);
                    dout.flush();
                    zwrotny.close();
                    dout.close();
                }
                if(komunikaty[0].equals("challenged")) {
                    System.out.println("Uzyskalem komende challenged");
                    System.out.println("Zostalem wyzwany przez: " + komunikaty[2]);
                    if(komunikaty[3].equals("0")) {
                        System.out.println("Przegralem");
                    } else {
                        System.out.println("Wygralem");
                    }
                }
                if(komunikaty[0].equals("results")) {
                    System.out.println("Uzyskalem komende results");
                    System.out.println("Moje mecze:");
                    for(int i = 1; i < komunikaty.length-1; i+=3) {
                        System.out.println(komunikaty[i] + " " + komunikaty[i+1] + " " + komunikaty[i+2] + " ");
                    }
                }

                socket.close();
                din.close();
                dout.close();
                msgin = "";
                komunikaty = null;
            } catch (IOException e) { 
                //e.printStackTrace(); 
            } 
   }

}
2
  • So, you have a compiler error. To determine its cause, one needs to see the source code being compiled, probably both classes. Commented Dec 2, 2018 at 20:54
  • ok, I added the source files Commented Dec 2, 2018 at 20:58

3 Answers 3

3

Ok, these classes are in packages, but your command line screens do not indicate that you have them in the folders that correspond to their packages. Rather than try to explain packages to you, I recommend that you remove the 'package' lines from both source files and try again.

Sign up to request clarification or add additional context in comments.

2 Comments

Now that I look at this again, I'm not sure why your delete command is executed on "src/pack/a/*.class" - your compile line ("java x.java") seems to expect the source files to be in the current directory, so I don't know how you expect the class files to end up in a different directory. So there may be more going here that I can't tell about.
A StackOverflow tip for you, since you appear to be new. If an answer is helpful, it is usual to give it an upvote. If there is an answer that is 'correct', it is usual to both upvote it and mark it as the "accepted answer". Upvotes and accepted answers are tracked by the system, they increase the reputation of people that answer them, and generally help the whole place run smoothly.
2

You need to put both files in a directory called a to match the package name.

1 Comment

Unfortunately, if OP does this, then he needs to change the compilation command to reflect the folder/directory the files are in. I recommended that he just get rid of the packages. Working in the 'default package' is not best practice for a project of any size, but it will allow him to get past the current error, and s/he will (no doubt) learn about packages later. Practically no one deals with packages using command-line tools these days, anyway.
1

Ok, thank you guys. The only thing I had to do was to remove the

package a;

line in both files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.