Asalas77
Asalas77

Reputation: 249

Export Runnable JAR file not working

I have read a lot of similar questions but I cant find anything helpful in them, so I have to ask one myself.

I have a program that uses TCP socket connection server and client with a total of 5 classes in one package, two of them have a main method (server and clientGUI).

enter image description here

But when I use rightclick on server.java -> export -> runnable jar file with server run configuration, it works, I can run it from outside eclipse. But when I do the same for clientGUI.java, nothing happens when I run the clientGUI.jar file.

main method in Server looks like this:

public static void main(String[] args) {
    Server s = new Server();
    s.start();
}

but in ClientGUI i only have this:

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

is that the problem? I don't have start() in ClientGUI, because nothing happens until user input, and everything related to graphics is coded in the constructor, and the rest is in ActionPerformed method that handles buttons.


ClientGUI:

public class ClientGUI extends JFrame implements ActionListener {

private static final long serialVersionUID = -1115273859077115660L;
private JTextField textField;
private JTextArea chatBox;

private static Client client;
private JButton resetB;
JButton btnConnect;
JLabel lblGrid;
JLabel lblGrid2;
static int port = 56667;
String IP;
private JTextField txtPort;
private JTextField txtIP;
final JButton readyB;

ClientGUI() {
    super("Client");
    getContentPane().setBackground(Color.LIGHT_GRAY);
    setResizable(false);
    setBounds(100, 100, 945, 750);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBackground(Color.WHITE);
    panel.setBounds(10, 11, 920, 520);
    getContentPane().add(panel);
    panel.setLayout(null);

    readyB = new JButton("READY");
    readyB.setEnabled(false);
    readyB.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            lblGrid.setVisible(true);
            readyB.setEnabled(false);
            client.sendMessage(new Message(Message.READY, client.myID, ""));
        }
    });
    readyB.setBounds(784, 461, 126, 48);
    panel.add(readyB);

    BufferedImage myPicture = null;
    try {
        myPicture = ImageIO.read(new File("img/grid.png"));
    } catch (IOException e) {

        e.printStackTrace();
    }
    lblGrid = new JLabel(new ImageIcon(myPicture));
    lblGrid.setText("");
    lblGrid.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent m) {
            int x = m.getX() / 40;
            int y = m.getY() / 40;
            if(client.myShots[x][y] == false){
            client.sendMessage(new Message(Message.SHOT, client.myID,
                    (x + 1) + " " + (y + 1)));
            client.myShots[x][y] = true;}
        }
    });
    lblGrid.setBounds(480, 40, 401, 401);
    panel.add(lblGrid);
    lblGrid.setVisible(false);

    lblGrid2 = new JLabel(new ImageIcon(myPicture));
    lblGrid2.setText("");
    lblGrid2.setBounds(40, 40, 401, 401);
    panel.add(lblGrid2);

    textField = new JTextField();
    textField.setBounds(493, 681, 437, 20);
    getContentPane().add(textField);
    textField.setColumns(10);
    textField.addActionListener(this);

    JPanel panel_1 = new JPanel();
    panel_1.setBounds(10, 542, 210, 159);
    getContentPane().add(panel_1);
    panel_1.setLayout(null);

    resetB = new JButton("R");
    resetB.setBounds(160, 108, 40, 40);
    resetB.addActionListener(this);
    panel_1.add(resetB);

    btnConnect = new JButton("Connect");
    btnConnect.setBounds(230, 542, 89, 23);
    getContentPane().add(btnConnect);
    btnConnect.addActionListener(this);

    JLabel lblIp = new JLabel("IP:");
    lblIp.setBounds(329, 546, 22, 14);
    getContentPane().add(lblIp);

    JLabel lblPort = new JLabel("Port:");
    lblPort.setBounds(329, 580, 46, 14);
    getContentPane().add(lblPort);

    txtPort = new JTextField();
    txtPort.setText("1234");
    txtPort.setBounds(361, 577, 120, 20);
    getContentPane().add(txtPort);
    txtPort.setColumns(10);

    txtIP = new JTextField();
    txtIP.setText("192.168.0.25");
    txtIP.setBounds(361, 542, 120, 20);
    getContentPane().add(txtIP);
    txtIP.setColumns(10);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(491, 544, 439, 124);
    getContentPane().add(scrollPane);

    chatBox = new JTextArea();
    scrollPane.setViewportView(chatBox);
    chatBox.setEditable(false);
    chatBox.setLineWrap(true);

    textField.requestFocus();

    setVisible(true);
}

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

void drawX(JLabel grid, int x, int y) {
    JLabel xg = (new JLabel(new ImageIcon("img/x.png")));
    xg.setBounds(40 * (x - 1), 40 * (y - 1), 40, 40);
    grid.add(xg);
    xg.setVisible(true);
    repaint();
}

void drawMiss(JLabel grid, int x, int y) {
    JLabel xg = (new JLabel(new ImageIcon("img/miss.png")));
    xg.setBounds(40 * (x - 1), 40 * (y - 1), 40, 40);
    grid.add(xg);
    xg.setVisible(true);
    repaint();
}

public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();

    if (o == textField) {
        String text = textField.getText();

        client.sendMessage(new Message(Message.CHAT, client.myID, text));
        textField.setText("");
    }

    if (o == resetB) {
    }

    if (o == btnConnect) {
        IP = txtIP.getText();
        port = Integer.parseInt(txtPort.getText());

        client = new Client(IP, port, this);
        // test if we can start the Client
        if (!client.start())
            return;
        else
        client.display("Connected!");
        txtIP.setEnabled(false);
        txtPort.setEnabled(false);
        btnConnect.setEnabled(false);
        readyB.setEnabled(true);
    }
}

public void append(String string) {
    chatBox.append(string);
    chatBox.setCaretPosition(chatBox.getText().length() - 1);
}
}

Client:

public class Client {
private ObjectOutputStream sOutput;
private ObjectInputStream sInput;
Socket socket;
private SimpleDateFormat sdf;
private static ClientGUI cg;
private static String IP;
private static int port;
int myID;
boolean[][]myShots = new boolean [10][10];

Client(String IP, int port, ClientGUI cg) {
    this.IP = IP;
    this.port = port;
    this.cg = cg;

    sdf = new SimpleDateFormat("HH:mm:ss");
}


public boolean start() {
    try {
        socket = new Socket(IP, port);
    } catch (Exception e) {
        display("Server not running!");
        return false;
    }
    try {
        sInput = new ObjectInputStream(socket.getInputStream());
        sOutput = new ObjectOutputStream(socket.getOutputStream());
    } catch (IOException e) {
        display("Exception creating new Input/output Streams: " + e);
        return false;
    }

    new ListenFromServer().start();

    return true;
}

void display(String msg) {
    cg.append(sdf.format(new Date()) + ": " + msg + "\n");
}

void sendMessage(Message msg) {
    try {
        sOutput.writeObject(msg);
    } catch (IOException e) {
        display("Exception writing to server: " + e);
    }
}

public void clearMyShots(int id) {
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
                myShots[i][j] = false;
}

class ListenFromServer extends Thread {
    public void run() {
        Message cm = null;
        while (true) {

            try {
                cm = (Message) sInput.readObject();

            } catch (IOException ioe) {
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            switch (cm.getType()) {
            case Message.ID:
                myID = Integer.parseInt(cm.getMessage());
                display("Your ID is " + myID);
                break;
            case Message.HIT:

                String[] partHit = cm.getMessage().split(" ");
                int x = Integer.parseInt(partHit[0]);
                int y = Integer.parseInt(partHit[1]);
                int whoShot = cm.getID();
                if (whoShot == myID) {
                    display("HIT: " + (char) (x + 64) + y);
                    cg.drawX(cg.lblGrid, x, y);

                } else
                    cg.drawX(cg.lblGrid2, x, y);
                break;
            case Message.SHOT:
                display("SHOT: " + cm.getMessage());

                break;
            case Message.MISS: {
                String[] partMiss = cm.getMessage().split(" ");
                x = Integer.parseInt(partMiss[0]);
                y = Integer.parseInt(partMiss[1]);
                whoShot = cm.getID();
                if (whoShot == myID) {
                    display("MISS: " + (char) (x + 64) + y);
                    cg.drawMiss(cg.lblGrid, x, y);
                } else
                    cg.drawMiss(cg.lblGrid2, x, y);
                break;
            }

            case Message.DESTROYED:
                break;
            case Message.CHAT: {
                display("Player " + cm.getID() + ": " + cm.getMessage());
                cm = null;

                break;
            }
            }

        }
    }
}
}

Upvotes: 2

Views: 916

Answers (2)

Asalas77
Asalas77

Reputation: 249

I found that the cause was not related to the main method. Following user @vanje i ran my program from windows command line and it showed IOException reading image files that I forgot to copy to new directory. I did and it is working

Upvotes: 3

Konstantin Komissarchik
Konstantin Komissarchik

Reputation: 29139

To start a Swing application, such as your GUI client, you need to have the following in your main method:

javax.swing.SwingUtilities.invokeLater
(
    new Runnable()
    {
        public void run()
        {
            new ClientGUI();
        }
    }
);

Here is a Hello World Sample that will help you understand the minimum necessary to start a Swing app.

Upvotes: 0

Related Questions