简体   繁体   中英

Making an executable jar file

Here is my program and it works fine but wont open once I have made it into a jar.

and the command to make it into a .jar file;

jar -cvfm chat.jar manifest.txt client.class

my program is fine, my manifest is exactly as should be soooooo.....idk please

help. every other program i make turns into a working .jar but not this one.

class client {

    public static Socket    s;
    public static JTextArea jta;
    public static String    server;
    public static String    name;

    public static void main(String args[]) throws Exception {

        Thread t = new Thread(new Runnable() {

            public void run() {
                try {
                    s = new Socket(server, 9000);
                    DataInputStream dis = new DataInputStream(s.getInputStream());
                    String str;
                    while ((str = dis.readUTF()) != null) {
                        jta.append(str + "\n");
                    }
                } catch (Exception e1) {
                }
            }
        });

        JFrame j = new JFrame("Test");
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setVisible(true);

        jta = new JTextArea();
        JScrollPane jsp = new JScrollPane(jta);
        j.add(jsp);

        JTextField jtf = new JTextField();
        jtf.setFont(new Font("", Font.BOLD, 14));
        j.add(jtf, BorderLayout.SOUTH);

        JPanel jp = new JPanel();
        jp.add(new JLabel("Name:"));
        JTextField jtfN = new JTextField();
        jtfN.setPreferredSize(new Dimension(180, 20));
        jp.add(jtfN);

        jp.add(new JLabel("Server:"));
        JTextField jtfS = new JTextField();
        jtfS.setPreferredSize(new Dimension(180, 20));
        jp.add(jtfS);
        int jop = JOptionPane.showConfirmDialog(null, jp, "Enter Username and Server.", JOptionPane.OK_CANCEL_OPTION);
        if (jop == 0) {
            name = jtfN.getText();
            server = jtfS.getText();
            t.start();
        }

        jtf.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {

            }

            public void keyPressed(KeyEvent e) {

            }

            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    try {
                        String data = name + ": " + jtf.getText();
                        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                        dos.writeUTF(data);
                        jta.append(data + "\n");
                        jtf.setText("");
                    } catch (Exception e1) {
                    }
                }
            }
        });

        ///////////////////////
        j.getContentPane().setPreferredSize(new Dimension(800, 500));
        j.pack();
        j.setLocationRelativeTo(null);

    }
}

Making .jar file from .java file

  1. First make sure that your file name and your class name are same. "client.java" where class is client
  2. Now create a manifest.txt file. and write this

    Main-Class: Your_Main_Class_name

    In your case it is

    Main-Class: client

    Now save the file

  3. make sure that your both file (.java and manifest.txt) on the same directory

  4. Open command prompt
  5. Go to that folder where you saved .java and manifest.txt file. Using command

    cd your_path

  6. Now compile your .java file using command

    javac client.java

  7. Now you have client.class file on the same folder

  8. Now to make .jar file use this command

    jar cfn client.jar manifest.txt client.class

  9. Now your client.jar file is created on the same directory

  10. to run .jar file use this command

    java -jar client.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM