0

大学用の簡単なチャットを作成する必要があり、NetBeans で実行しようとすると、「メイン クラスが見つかりません」と表示されます。わかりません。メインクラスがあると思いますので、どこに問題があるのか​​教えてもらえますか? コードは次のとおりです。

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

public class server {
    private static ServerSocket socketservidor = null;
    private static Socket socketcliente = null;
    private static final int maxclientes = 4;
    private static final clienteThread[] hilos = new clienteThread[maxclientes];

    public static void main(String args[]) {
        int puerto = 2222;
        if (args.length < 1) {
            System.out.println("CONEXION REALIZADA CORRECTAMENTE \n"
                    + "CHAT INICIADO CORRECTAMENTE \n" + "NUM. PUERTO="
                    + puerto);
        } else {
            puerto = Integer.valueOf(args[0]).intValue();
        }

        try {
            socketservidor = new ServerSocket(puerto);
        } catch (IOException e) {
            System.out.println(e);
        }

        while (true) {
            try {
                socketcliente = socketservidor.accept();
                int i = 0;
                for (i = 0; i < maxclientes; i++) {
                    if (hilos[i] == null) {
                        (hilos[i] = new clienteThread(socketcliente, hilos))
                                .start();
                        break;
                    }
                }
                if (i == maxclientes) {
                    PrintStream oc = new PrintStream(
                            socketcliente.getOutputStream());
                    oc.println("Servidor ocupado. Vuelve a intentar más tarde");
                    oc.close();
                    socketcliente.close();
                }
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }
}

class clienteThread extends Thread {

    private PrintStream salida = null;
    private DataInputStream entrada = null;
    private int maxclientes;
    private final clienteThread[] threads;
    private Socket socketcliente = null;

    public clienteThread(Socket socketcliente, clienteThread[] threads) {
        this.socketcliente = socketcliente;
        this.threads = threads;
        maxclientes = threads.length;
    }

    public void run() {
        int maxclientes = this.maxclientes;
        clienteThread[] threads = this.threads;

        try {
            entrada = new DataInputStream(socketcliente.getInputStream());
            salida = new PrintStream(socketcliente.getOutputStream());
            salida.println("Solo nos falta saber tu nombre para empezar:");
            String nombre = entrada.readLine().trim();
            salida.println("Bienvenido a nuestro chat " + nombre + "\n"
                    + "Ya puedes chatear con otros usuarios!" + "\n"
                    + " teclea /salir para abandonar chat");
            for (int i = 0; i < maxclientes; i++) {
                if (threads[i] != null && threads[i] != this) {
                    threads[i].salida.println("***" + nombre
                            + " se ha conectado!!!***");
                }
            }
            while (true) {
                String linea = entrada.readLine();
                if (linea.startsWith("/salir")) {
                    break;
                }
                for (int i = 0; i < maxclientes; i++) {
                    if (threads[i] != null && threads[i] != this) {
                        threads[i].salida.println(">>" + nombre + ":" + linea);
                    }
                    if (threads[i] != null && threads[i] == this) {
                        threads[i].salida.println("YO:" + linea);
                    }
                }
            }
            for (int i = 0; i < maxclientes; i++) {
                if (threads[i] != null && threads[i] != this) {
                    threads[i].salida.println("***" + nombre
                            + " se ha desconectado***");
                }
            }
            salida.println("***Te has desconectado del chat***");

            for (int i = 0; i < maxclientes; i++) {
                if (threads[i] == this) {
                    threads[i] = null;
                }
            }
            entrada.close();
            salida.close();
            socketcliente.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}
4

2 に答える 2

0

これを試して。

プロジェクト フォルダーを右クリックし、[プロパティ] をクリックして、左側のメニューから [実行] を選択します。右側にメイン クラスが表示されます。[参照] をクリックして、サーバー クラスを選択します。その後、プロジェクトを実行すると、エラーは発生せず、サーバーが起動します。

于 2013-03-18T11:54:19.593 に答える
0

ファイル名は Java クラス名と同じでなければなりません。たとえば、クラス名が「sever」の場合、ファイルは server.java という名前にする必要があります。日食で昼食をとることができました。

于 2013-03-18T11:32:55.113 に答える