I have a Java application launched via Webstart. About half the time, this program will not die. This program listens on a socket and when it receives a particular command, should close down. When it receives this command, it prints out what it normally should to the java console (that it received the shutdown command), the Java console closes, all the windows close, but the java process continues to run.
I have tried switching System.exit(0) to Runtime.getRuntime().halt(0) with no success - the same problem happens (my initial thought was a shutdown hook was affecting it).
Furthermore, when I have VisualVM connected to the Java process, it also indicates that it loses the connection when halt or exit are called, and I cannot reconnect, but the java process continues to exist.
Any ideas on how I could solve this? I've tries wrapping the program in try { } and exit/halt in finally { } to ensure nothing was holding it up, tried halting instead of exiting, etc...
My only thought is Java could be retaining a system resource (file handle to socket or something) or be stuck in a system call so the system won't let it quit - but the Java console closes, which to me seems like an indication the VM is trying to shutdown...
Any thoughts or ideas are appreciated!
Related
I'm developing a Java desktop application for Windows and I'm trying to implement the Windows Restart Manager. I'm successfully receiving the Windows messages for exiting the application and when I send them manually, it just works and the application shut downs.
When I do it as part of the uninstall process, the shutdown procedure in my application runs and I can even see it calls System.exit(0) but even after that, there's a process that doesn't stop. My Java application is packaged into an executable file using launch4j in case that's relevant.
The way I know System.exit(0) is being called is because I'm dumping debugging information to a file and I print out that System.exit(0) is about to be call and I can see that whether the application succeeds or fails on properly shutting down.
Using the Process Explorer I can see the javaw.exe sub-process and when the exit procedure happens, that goes away, but the parent process remains. While the is running, it looks like this:
and after the failed exit, it looks like this:
If I have a remote debugger connected to the process, at this point, the debugger gets disconnected.
What could be causing this?
I'm close to being convinced this is a bug in launch4j, so, I reported it here: https://sourceforge.net/p/launch4j/bugs/185/
I am running a Java process with several threads from a Powershell on Windows Server in Admin Mode.
Sometimes it randomly freezes until I press Ctrl+C, then it just picks up the work again.
Has anyone ever come across this problem and point me to where to look at?
If you start the process with Runtime.exec(..), it is sometimes necessary to read Bytes from the Process.getInputStream() resp. Process.getErrorStream(), else the process blocks, when it tries to write more to std-out (or std-err) than a certain buffer size.
I had this problem often, when starting Shell scripts.
You can create a Background Thread to read periodically from these streams.
Alternatively and more easy, you can use the ProcessBuilder class to start the shell process and use "inheritIO()" method.
My project is basically a Communication client like lync which is developed in JAVA for front end (GUI) and uses platform specific native (C or C++) code for running services.
Now, on Linux, (Ubuntu 12.04), once the JAR application is invoked, it loads all the native code shared libraries and the UI thread starts executing. Any action done in the UI will throw an event to the native code which is in C. So currently i need to debug a crash in a C/C++ user library which is triggered when i do something in a UI drop down.
I am using GDB, to attach to the PID of the process , (sudo gdb -p ), all the symbols are loaded and i am able to set a breakpoint to a function say A() in the library.After continue command in GDB, i select the instance from UI Dropdown and breakpoint is hit at Function A(). At this moment, my ubuntu machine hangs and no keyboard interrupts are working. I am only able to move my mouse pointer but cannot click on anything.
However, to verify that kernel is not down, i can ping the machine and even SSH is possible. Once the same GDB is invoked by SSH the above problem is not encountered. May anyone please help me out here as to why UI or X11 process hangs during the above scenario.
PS: Yes there are lot of threads running, it might be a thread deadlock situation but it does not happen when GDB is invoked by SSH terminal.
Thanks and Regards,
Indra
why UI or X11 process hangs during the above scenario
As Mark Plotnick correctly pointed out, the X11 process does not hang. Rather, it grabs the keybard (all keyboard events are dispatched to it), and can not release that grab (it is stopped by GDB before it reaches the release point).
There are two common solutions:
ask the application to not do the keyboard grab (as Mark said), or
debug the application from a separate machine (this can even be done on single physical machine: just run the application inside a VM).
P.S. Why do application menues grab keyboard? Because hitting Esc usually dismisses the menu, and they want to see that Esc regardless of whether the application has input focus or not).
I have a Java server running on an Ubuntu machine and every few days it just... dies. Every exit point on every thread leaves a log entry and everything is wrapped in try-catch with a log entry in the catch, but on these mysterious shutdowns, nothing gets logged.
I launch the server with a simple nohup java -jar MyServer.jar command.
One time, I had a terminal I launched it from still open when this happened and the word "KILLED" showed up there. Note that, when I kill the server myself with a kill command from another terminal, nothing shows up.
How do I determine what killed it?
There's probably other ways of doing this but I'd like to use an empty file to have one instance of an application running at a given time. This would be done by creating the file when the application is launched and have other application instances exit as soon as they detect the file.
The trouble with this approach is that the file can remain if the application stops unexpectedly and a ShutDownhook is proving unreliable.
How would you go about making this work as intended?
Bind to a high numbered port but don't listen. Two programs can't bind to the same TCP port on the same machine. Very cross-platform but still somewhat of a kludge.
Create the file and keep it open with an exclusive lock (that is, don't pass FILE_SHARE_READ, etc). When the second instance starts up, it tries to open the file and if it fails it means the first is still running.
If the first crashes, then Windows will automatically close all file handles and so the second process will come along, see that the file is there but since it can open it then it knows the first has crashed (this technique could also be used for a special dialog, "I see the previous instance of this application crashes, would you like to restore your last session?" or something)
You could always wrap your Java program in another program (doesn't need to be complicated - could even be a shell script) that would detect abnormal exit and delete the file. e.g.
if(!`java MyProgram`){
rm lockFile
}
This may be a little much. But you can start a ServerSocket and bind to some arbitrary port that each application knows. If the port is available the application wins to start up, if not a binding exception is thrown and the application gracefully stops.
There is probably a better way to do this than using a file but with that approach you could write a timestamp to the file and update it at regular intervals using a Timer. Then when your program starts it could compare the timestamp in the file with the current time and quit if it is too close. This guarantees that your program will be able to restart no matter how it was terminated.
Assuming the lock will be released when the application exits and deleting the file on startup does the trick. See the link below.
http://jimlife.wordpress.com/2008/07/21/java-application-make-sure-only-singleone-instance-running-with-file-lock-ampampampampamp-shutdownhook/
P.S: I've tried using delete when the application exits but it seems to fail.