Detect windows logout event in Java application
Asked Answered
I

4

2

Is there a library that I can use with Java to listen for user logout and possibly other Windows events? (Even better if it supports multiple platforms!)

I remember reading about a library of this sort a number of years ago, but can't seem to find it now. I've seen other threads to do essentially the same thing using Python with win32ts.

Also better if it's free and/or open source.

Thanks.

Note: The candidate solution of using Runtime.getRuntime().addShutdownHook(Thread) does not work correctly with javaw. I am still looking for a solution that will work with javaw. See java bug ids 4486580 and 4302814. Thanks --cam

Incomprehension answered 4/2, 2009 at 18:2 Comment(0)
I
7

I think you want to look at Runtime.addShutdownHook(). This gives you the last-ditch opportunity to do things before the JVM shuts down. This lets you detect an actual logout where all of the applications are shutdown, but not they just temporarily switch users, leaving their state ready to come back to. However, as a warning, whatever you plan on doing during the shutdown process needs to be brief since the OS may SIGKILL (or the equivalent) on you at any time, which halts the JVM immediately.

If this isn't what you need, you're probably going to need to use JNI to register a native listener. As for the other windows events, the AWT library provides a number of classes for implementing the listeners that most normal apps would be interested in. In this case, you might be interested in a FocusEvent.

Incoordinate answered 4/2, 2009 at 18:28 Comment(0)
D
3

My best guess would be to use a ShutdownHook. You can register a thread which is invoked as soon as the JVM is beeing shut down.

Edit:
If you want to realize this under windows, you could create an invisible window and react on the WM_QUERYENDSESSION message which is sent by the OS upon user log off or system shutdown.

Developing answered 4/2, 2009 at 18:21 Comment(2)
Unfortunately, using a shutdown hook in Runtime does not work when using javaw.Incomprehension
It seems to be a known bug, see hereDeveloping
S
2

There seems to be a lot of people that are running into this issue. Although, the people on the Java project obviously don't consider this much of a priority (The original ticket was opened almost 10 years ago).

I have had some success with the work-around posted in bug 4486580 which uses the hidden JWindow. However, this work-around is considered unreliable. I also found this little bit of discussion started by cowwoc who posted the work-around: http://forums.java.net/jive/thread.jspa?threadID=46716. This work-around is better than nothing, but it would be nice to block a windows logout until the program has definitely finished cleanup. You don't want to be part-way through the process when the process is terminated.

The most recent bug opened surrounding this issue is 6798985. It seems to have gone mostly unnoticed.

Scandal answered 11/12, 2009 at 21:23 Comment(1)
Provided link is dead!Ethanethane
B
0

It's 2023 and there's still no standard API for this, but using a custom sun.misc.SignalHandler for SIGTERM will allow your application to run some code before logout or shutdown: https://mcmap.net/q/103276/-how-do-i-get-my-java-application-to-shutdown-nicely-in-windows

Bobine answered 22/9, 2023 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.