Reputation: 35
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);
}
}
Upvotes: 2
Views: 3189
Reputation: 1596
Making .jar file from .java file
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
make sure that your both file (.java and manifest.txt) on the same directory
cd your_path
Now compile your .java file using command
javac client.java
Now you have client.class file on the same folder
Now to make .jar file use this command
jar cfn client.jar manifest.txt client.class
Now your client.jar file is created on the same directory
to run .jar file use this command
java -jar client.jar
Upvotes: 4