Java 理解命令行参数

Java 理解命令行参数,java,args,Java,Args,基本上我只是在写一个套接字。 但由于某种原因,我一直都会遇到这个错误 线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常:0 位于Main.ChatClient.Main(ChatClient.java:143) 哪一行是“字符串服务器=args[0];” args需要是什么来修复此问题 package Main; import javax.swing.*; import java.awt.*; import j

基本上我只是在写一个套接字。 但由于某种原因,我一直都会遇到这个错误

线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常:0 位于Main.ChatClient.Main(ChatClient.java:143)

哪一行是“字符串服务器=args[0];”

args需要是什么来修复此问题

package Main;


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.util.Observable;
    import java.util.Observer;


// Class to manage Client chat Box.
public class ChatClient {

Main a = new Main();

/** Chat client access */
static class ChatAccess extends Observable {
    private Socket socket;
    private OutputStream outputStream;

    @Override
    public void notifyObservers(Object arg) {
        super.setChanged();
        super.notifyObservers(arg);
    }

    /** Create socket, and receiving thread */
    public void InitSocket(String server, int port) throws IOException {
        socket = new Socket(server, port);
        outputStream = socket.getOutputStream();

        Thread receivingThread = new Thread() {
            @Override
            public void run() {
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(socket.getInputStream()));
                    String line;
                    while ((line = reader.readLine()) != null)
                        notifyObservers(line);
                } catch (IOException ex) {
                    notifyObservers(ex);
                }
            }
        };
        receivingThread.start();
    }

    private static final String CRLF = "\r\n"; // newline

    /** Send a line of text */
    public void send(String text) {
        try {
            outputStream.write((text + CRLF).getBytes());
            outputStream.flush();
        } catch (IOException ex) {
            notifyObservers(ex);
        }
    }

    /** Close the socket */
    public void close() {
        try {
            socket.close();
        } catch (IOException ex) {
            notifyObservers(ex);
        }
    }
}

/** Chat client UI */
static class ChatFrame extends JFrame implements Observer {

    private JTextArea textArea;
    private JTextField inputTextField;
    private JButton sendButton;
    private ChatAccess chatAccess;

    public ChatFrame(ChatAccess chatAccess) {
        this.chatAccess = chatAccess;
        chatAccess.addObserver(this);
        buildGUI();
    }

    /** Builds the user interface */
    private void buildGUI() {
        textArea = new JTextArea(20, 50);
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        add(new JScrollPane(textArea), BorderLayout.CENTER);

        Box box = Box.createHorizontalBox();
        add(box, BorderLayout.SOUTH);
        inputTextField = new JTextField();
        sendButton = new JButton("Send");
        box.add(inputTextField);
        box.add(sendButton);

        // Action for the inputTextField and the goButton
        ActionListener sendListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str = inputTextField.getText();
                if (str != null && str.trim().length() > 0)
                    chatAccess.send(str);
                inputTextField.selectAll();
                inputTextField.requestFocus();
                inputTextField.setText("");
            }
        };
        inputTextField.addActionListener(sendListener);
        sendButton.addActionListener(sendListener);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                chatAccess.close();
            }
        });
    }

    /** Updates the UI depending on the Object argument */
    public void update(Observable o, Object arg) {
        final Object finalArg = arg;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                textArea.append(finalArg.toString());
                textArea.append("\n");
            }
        });
    }
}

public static void main(String[] args) {
    System.out.println("troll");
    String server = args[0];
    System.out.println("reached here");
    int port =2222;
    ChatAccess access = new ChatAccess();

    JFrame frame = new ChatFrame(access);
    frame.setTitle("MyChatApp - connected to " + server + ":" + port);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    try {
        access.InitSocket(server,port);
    } catch (IOException ex) {
        System.out.println("Cannot connect to " + server + ":" + port);
        ex.printStackTrace();
        System.exit(0);
    }
}
}
鉴于:

我建议您提供服务器名称作为程序的第一个参数

args是传入的命令行args数组。

给定:

String server = args[0];
我建议您提供服务器名称作为程序的第一个参数


args是传入的命令行args数组。

。。。和
ArrayIndexOutOfBoundsException:0
表示数组中没有第0个元素,这意味着程序的调用方不提供命令行参数。。。和
ArrayIndexOutOfBoundsException:0
表示数组中没有第0个元素,这意味着程序的调用方不提供命令行参数
public static void main(String[] args)