LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Program closes fine when run from cmd but .jar doesn't close. (http://www.linuxquestions.org/questions/programming-9/program-closes-fine-when-run-from-cmd-but-jar-doesnt-close-4175417113/)

GanglyCloth 07-17-2012 01:47 AM

Program closes fine when run from cmd but .jar doesn't close.
 
Hello,

I`m developing a simple java application and I've run into a rather annoying issue. When I run my application from the command line using > java Main it behaves as expected, specifically when I close the window the program exits gracefully. However when I build it as a jar using > jar cfe app.jar Main Main.class Form.class it doesn't do anything when I hit the close button and I have to kill the process.

The app simply displays a randomly selected message if the app hasn't been run earlier in the day.

This is my main method.
Code:

public static void main(String args[])
{
    String message = "";
    boolean usedToday = false;
    usedToday = usedToday();
    message = getMessage(usedToday);
    Form.draw(message);
}

And the form class
Code:

public class Form {
    public static void draw(String message) {
        final Frame frm = new Frame("App");
        Label lbl = new Label(message);
        frm.add(lbl);
        lbl.setAlignment(Label.CENTER);
        frm.setSize(500, 200);
        frm.setBackground(Color.CYAN);
        frm.setVisible(true);
        frm.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                Main.writeTime();
                frm.dispose();
                System.exit(0);
            }
        });

    }
}


GanglyCloth 07-17-2012 02:03 AM

The solution was simply to include the class file Form$1.class in the jar
>jar cfe app.jar Main Main.class Form.class Form$1.class
or
>jar cfe app.jar Main *


All times are GMT -5. The time now is 12:18 AM.