LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting Java program to close properly on logging off kde (https://www.linuxquestions.org/questions/programming-9/getting-java-program-to-close-properly-on-logging-off-kde-671352/)

arubin 09-21-2008 05:37 AM

Getting Java program to close properly on logging off kde
 
I have an accounts program that I have written in Java which I run in kde.

When I close the program all data is saved overwriting the old data file.

If I log off kde without closing the program down first then any changes to the data are lost.

How do I get the program notified of the log off so that it can close down gracefully?

Thanks,

Alan

rjlee 09-21-2008 11:40 AM

According to a Google search, if you create a directory called ~/.kde/shutdown, and put a program in there, it will be run on shutdown. (http://linux.derkeiler.com/Mailing-L...4-08/2313.html)

To use that, You would need to write a program that would connect to your program and tell it to save its data. This might be a little more difficult in Java, depending on how you're running the program, but there are some IPC tools to help you with that (http://www.velocityreviews.com/forum...c-in-java.html).

However, nothing beats auto-saving: if you kill the X server with ctrl+alt+backspace, then all your programs will die, and if the program crashes then you've lost all your data anyway. My advice would be to periodically save to a fixed auto-save file, and then load from that if it exists. (This is more or less what Firefox does; even if you kill the program, you can still start it again right from where you left off.) Of course, that could be impractical for your application since I don't know what size of program you're dealing with.

Hope that helps,

—Robert J Lee

jay73 09-21-2008 01:40 PM

You could create a temporary file to store new data and use flushing to make sure that all data are written out as soon as possible. Then, when you restart the application, you could have an option to overwrite the original with the last temporary file, clear the temporary file or create a new one, etc. As rjlee says, there is a lot that can go wrong in between logging on and off.

arubin 09-22-2008 03:19 AM

Thanks. The answers are not quite what I was looking for. No doubt autosaving and temporary backups are a good idea but other programs seem to 'know' that you are logging off and prompt you to save data.

This is a further prompt for me to take a very different approach and put all my data in a mysql database so everything is updated as the program runs.

Alan


All times are GMT -5. The time now is 11:00 AM.