Java compiling and running a program which contains 2 classes in command line

Maks Gajewski :

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

}
arcy :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=116756&siteId=1