无法加载或找到主类

时间:2019-05-23 08:39:17

标签: java netbeans

我正在通过Netbeans 8.0.2在Java上开发客户机-服务器体系结构。当我的服务器已经在本地网络中的后台运行时,当我尝试通过Powershell终端运行客户端文件时,我不断收到错误消息“无法加载或找到主类ClientChat”。

我尝试了许多解决方案,例如使用“ -cp”执行文件。参数,更改环境变量的路径或在Netbeans终端上运行它,但是什么也没有。 要运行该应用程序,我尝试使用“ java -cp。ClientChat 192.168.137.1 3000”和java ClientChat 192.168.137.1 3000”。 我的路径变量设置为:C:\ Program Files \ Java \ jdk1.8.0_111 \ bin。 我的JRE版本是:jre1.8.0_211。

这是我的客户代码(如果有帮助的话)。

package clientchat;

import java.net.*; import java.io.*; import java.awt.*;
import java.awt.event.*; import javax.swing.*;

public class ClientChat extends JFrame implements Runnable, ActionListener{
    protected DataInputStream i=null;
    protected DataOutputStream o=null;
    Socket s=null;
    protected String nome= "";
    protected JLabel l;
    protected JTextArea output;
    protected JTextField input;
    protected JList scelta;
    DefaultListModel listModel = new DefaultListModel();
    protected Thread ascolta= null;
    protected char separatore= '\n';
    static String host = null;
    static String porta = null;
    volatile boolean continua = true;

    public ClientChat(String titolo) {
        super(titolo);

        JPanel pannello=new JPanel();
        pannello.setLayout(new BorderLayout());

        setFont(new Font("Helvetica", Font.PLAIN, 14));
        setLayout(new BorderLayout());
        output= new JTextArea("", 10, 10);
        JScrollPane sp = new JScrollPane(output);
        add(sp,"Center");
        output.setEditable(false);


        listModel.addElement("Tutti");

        scelta= new JList(listModel);
        scelta.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        scelta.setSelectedIndex(0);
        JScrollPane sp2 = new JScrollPane(scelta);
        pannello.add(sp2,"Center");

        add(pannello,"East");

        l= new JLabel("Destinatario:");
        pannello.add(l,"North");
        input= new JTextField();
        input.setEditable(false);
        input.addActionListener(this);
        add(input,"South");
        ascolta= new Thread(this);
        ascolta.start();
    }

    public void termina() {
        if(ascolta != null) 
            continua = false;
        ascolta= null;
        try { 
            if (o != null) 
                o.close();
        }
        catch (IOException ecc){
            ecc.printStackTrace();
        }
        try { 
            if (i != null) 
                i.close();
        }
        catch (IOException ecc){
            ecc.printStackTrace();
        }
        try { 
            if (s != null) 
                s.close();
        }
        catch (IOException ecc){
            ecc.printStackTrace();
        }
    }

    public void run(){
        try {
        output.append(" G/L Chat di Gentile Francesco e Lucarelli Marco\n\n");
        output.append("Puoi inviare messaggi alla chat scrivendo nella riga in basso e poibattendo INVIO. \n");
        output.append("Puoi scegliere dalla lista a destra il destinatario dei messaggi cheinvii.\n\n");
        output.append("Connessione al server in corso... ");
        s= new Socket(host, Integer.parseInt(porta));
        i= new DataInputStream(new BufferedInputStream(s.getInputStream()));
        o= new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
        output.append("ok\n\nScrivi il tuo nome:\n");
        input.requestFocus();
        input.setEditable(true);

        String selezionato= "";
        String riga= "";
        String str;
        int primo, prossimo, n;
        String parole [];
        int posizione;
        riga= i.readUTF(); //come primo messaggio il server rimanda il nome eventualmente modificato con gli asterishi
        nome = riga;
        setTitle("Esempio chat: "+nome);

        while(continua) {
            riga= i.readUTF();
            if (riga.indexOf(separatore)== -1){
                output.append(riga + '\n');} 
            else{ 
                parole = riga.split("\\n");
                output.append(parole[0] + '\n');
                selezionato= (String) scelta.getSelectedValue();
                listModel.removeAllElements();
                listModel.addElement("Tutti");
                scelta.setSelectedIndex(0);
                posizione = 0;

                for (n = 1; n < parole.length; n++){
                    if (!nome.equals(parole[n])){
                        listModel.addElement(parole[n]);
                        posizione ++;

                        if (parole[n].trim().equals(selezionato.trim()))
                            scelta.setSelectedIndex(posizione);
                    }
                }
            }
        }
        }
        catch (Exception ecc){
            output.append("ERRORE!!!");
            output.append(ecc.getMessage());
            termina();
            return;
        }
    }

    public void actionPerformed(ActionEvent e) {
        if ((e.getSource() instanceof JTextField) && ((JTextField) e.getSource() == input) &&
        ((input.getText() != ""))){
            String msg;
            String testo = input.getText();

            if (nome.equals("")){
                nome= testo;
                output.setText("");
                msg= nome;
            } 
            else {
                output.append("> " + testo + '\n');
                msg= (String) scelta.getSelectedValue() + separatore + testo;
            }
            try {
                o.writeUTF(msg);
                o.flush();
            } 
            catch (IOException ecc) {}
            input.setText("");
        }
    }

    public void dispose (){
        termina();
        super.dispose();
    }

    public static void main (String args []){
        if (args.length >= 1)
            host= args[0];
        else
         host= "localhost";

        if (args.length >= 2)
         porta= args[1];
        else
            porta= "3000";

        ClientChat finestra = new ClientChat("Esempio chat");
        finestra.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        finestra.setSize(600,400);
        finestra.setVisible(true);
    }
}

说实话,这似乎与路径有关,但我无法解决。 我知道这个问题可能是this one的重复,但是我在那里找不到合适的解决方案。

0 个答案:

没有答案