Public Group

# What is the recommended correct way of exiting Java applications that's also easy to learn

This topic is 2690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have done some degree of researching on exiting Java in a more general, error-free way.

I have seen some articles found in Google regarding this problem, but it's not surprising that some codes which recommended using WindowListener() tends to be much harder for some of us in class to learn.

In my Java class, I have a textbook, "How to Program in Java, 7th edition", that all of my classmates use. In it, it mentioned the following codes are used to close the application when executed from the operating system terminal/console.

[source lang="java"]
public static void main(String arg[])
{
//Creation of a new application.
JFrame app = new JFrame();
//We do something to this application.

//Which we will ignore, since it's irrelevant.

//And we set the close method here.
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
[/source]

This is what my classmates learned from the book, and what they are currently using. I, myself, rely on Eclipse IDE. And I noticed that, even if I put in these codes, I'll still receive Java errors. I'm using Windows 7, if that helps out. At my school, all computers use Windows XP SP3.

[source lang="java"]
// ---- Something I will place here in a moment ----
// <reserved>
// ---- End of placement holder -----
system.Exit(0);
return;
[/source]

For that reserved placeholder, the things I'm confused about are:

1) The dispose() method usage.
2) Which "dispose()" should I use, "app.doDispose()" or "app.dispose()"?
4) Should I follow along what the book tells me to do in Eclipse IDE?

That's about it. I probably will teach these would-be-knowledge answers to other classmates who are not quite catching up. Thanks in advance.

##### Share on other sites

[source lang="java"]
public static void main(String arg[])
{
//Creation of a new application.
JFrame app = new JFrame();
//We do something to this application.

//Which we will ignore, since it's irrelevant.

//And we set the close method here.
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
[/source]

That would indeed be a correct way to let the Swing thread exit. I would suggest using JFrame.DISPOSE_ON_CLOSE instead though. It won't make a difference in a single-frame-environment but will probably lead to the more likely desired behavior in a multi-frame-environment (that is, not terminating the whole application when the first frame is closed).

That aside, I have no idea what you mean by "still receive Java errors". I can only guess that you do something wrong in your code, which triggers an exception, which gets printed to the console. In this case you can either catch and handle the exception yourself (since there will usually be a very good reason for an exception to be thrown, I would not recommend that) or fix the error. The stack trace included in each exception will tell you exactly where it was triggered.

##### Share on other sites
All I ever done was put these two lines of code inside main(), and still get an error from the operating system.

Although Eclipse said nothing, Windows 7 told me that Java had experienced an unexpected error.

##### Share on other sites
Do you have a JDK installed? Has Eclipse been told to use the right JDK? Have you done any manual manipulations in the JDK directories? Can you start non-Swing applications (for example just printing "Hello World" to the console)?

##### Share on other sites

Do you have a JDK installed? Has Eclipse been told to use the right JDK? Have you done any manual manipulations in the JDK directories? Can you start non-Swing applications (for example just printing "Hello World" to the console)?

1. Yes. Java SE 6.
2. Yes. I even specified the right directory to it, and it didn't say that it couldn't find it. That means, it's correct.
3. No. I haven't done any manual manipulations in the JDK. All I know, those are hard to fix, so I leave it alone.
4. Yes. I can do simple input and output without any errors.

##### Share on other sites
So you get an access violation the moment you are trying to use Swing? Even if you do nothing except creating a frame and making it visible?

What happens if you do something like this:

public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { System.out.println("Printing from Swing's thread"); } }); }

##### Share on other sites

So you get an access violation the moment you are trying to use Swing? Even if you do nothing except creating a frame and making it visible?

What happens if you do something like this:

public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { System.out.println("Printing from Swing's thread"); } }); }

1. How do I tell that Java had access violations in Swing?

2. If I do only just those two, the application would not completely terminate. I have to press the "Stop" button in the Debug tab for it to stop. I can narrow it down to creating a JFrame variable and leaving it there without doing anything gives me an error.

[source lang="java"]
public static void main(String args[])
{
JFrame app = new JFrame();
}

[/source]

3. If I put in the code you've posted in Eclipse, I get no errors.

##### Share on other sites

1. How do I tell that Java had access violations in Swing?

You told me a Java program runs fine without access violations unless you try to create a JFrame.

2. If I do only just those two, the application would not completely terminate. I have to press the "Stop" button in the Debug tab for it to stop. I can narrow it down to creating a JFrame variable and leaving it there

These two what? How can you be in a situation where you have to press "Stop" if your program crashes as soon as you start? When exactly does it crash? What exactly are you doing to make it crash?

##### Share on other sites

You told me a Java program runs fine without access violations unless you try to create a JFrame.

Hm... That also explains why Windows 7 detected unexpected errors whenever I finish playing Minecraft. Yes, unless I try to create a JFrame, I don't get any crashes.

These two what? How can you be in a situation where you have to press "Stop" if your program crashes as soon as you start? When exactly does it crash? What exactly are you doing to make it crash?

All I did was instantiate a JFrame variable, and set it visble. I didn't set it to EXIT_ON_CLOSE, therefore, the program would hang after I pressed the "Close" button. Since it's hanging, I have to press "Stop" for the Java to stop running it, and kill the process. By hanging, I meant the JFrame would close, making it invisible, but the process is still running. I need to "Stop" its process in order to completely exit out of the program, thereby interrupting the process of invoking a crash that will be detected by Windows 7.

When I instantiate a JFrame variable and set it visible, I do not crash. Nor do I crash when I press the "Close" button. It's something more related to exiting out of the process, do the Java unexpected errors pop/show up. Other than that, I don't know what else I can do to invoke a crash.

For it to crash, I need to set its default close action to EXIT_ON_CLOSE in the setDefaultCloseOperation(). Then, whenever I show the JFrame up, and letting it visible, and then pressing the "Close" button, the crash would then always happen.

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
16
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632580
• Total Posts
3007188

×