Killing background process - java

I am running a Java program that acts as a server for my program using CreateProcess with no window (using the settings shown here). I have an object which runs the process in its constructor, saves the process handle, and uses it in the destructor in a call to TerminateProcess to kill it.
My problem is that when my window is closed with the X in the corner, this destructor is never called and the process keeps running in the background, the handle is lost, and I have no indication that it is still running or a way to kill it (other the Task Manager). How can I make sure it is killed.
Larger picture
Here is what I need to do. If you can suggest a better way altogether to accomplish this, that is fine too.
I need a new instance of this java server for every run of my program (sometimes even multiple times in the program). I would like to run it without any visible indication (window etc.) but this is not a requirement. I do need to be able to either make sure that no matter what happens the server I opened is killed when I am done with it, or a way to make sure there is no running instance of it and if so, kill it before I start a new one. (Or better yet, both.) Using the process name is no good, since it is just java, and I don't want to kill all Java instances on the computer. I can (if I knew how) check if a program is listening on the port this server uses, and if so close it.
EDIT
I forgot to mention (if it makes a difference) that the GUI is C# and I have no control over it or over the Java program itself. I am writing a library in c++ that get called from the C# GUI and needs to use the Java server.

Create a new background process called "watchdog". This process obtains a handle to your main GUI and also to the background Java server, and then calls WaitForSingleObject on your GUI's process handle. When this call returns, you terminate the Java server.

Modify your DllMain entrypoint to handle DLL_PROCESS_DETACH. Its response should be to perform whatever desired cleanup you have.

Related

How can we make sure that only one instance of any application in running even in 2 different JVM's

I was asked this question at an interview.
Lets say VLC player is there and running on our OS.
Now when we again try to launch VLC , that should not happen.
alternatively, say we have two command windows, we runn same java program in two different Windows. How can we ensure only one runs.
Is this OS dependent thing?
Whats implementation?
I read something about MUTEX/Semaphore but I guess that is inside one JVM only.
You could write a pid file to disk, in a known location, indicating that the application is already running. Then, on startup, look for that file. If it's there, the application is already running, so exit.
Left as an exercise to the reader: ensuring that the pid file is removed even when the app crashes.
One possible answer would be to open and listen on a specific (and uncommon) tcp server port on start-up, since only one process can hold the socket at a time a second instance could detect the socket can't be listen'd and then exit with an error. Alternatively, you could potentially create a File on startup (and exit if it exists).

Opening a new command prompt and writing to it in Java

The idea behind this is that I have a main class that acts as an agent, constantly running and waiting for instructions. Then, the agent gets instructed to launch n instances of another class which plays a monitoring role.
Since each instance of the monitoring class is going to be doing heavy printing (plus the fact that the agent class also prints a little bit), I would like for each instance to have its own command window to do all its printing to.
Is this possible? If not, I welcome suggestions on how to get a similar effect.
Thank you.
EDIT: I feel like some clarification is in order. I want to start a new command/terminal window per monitoring instance and regularly write to that window.
I would obviously love to be able to run this on any machine, but at least I'd like to it to be able to work on Windows.
I know that there are some GUI libraries available (AWT, Swing) but I want the application to be as lightweight as possible, so that I can maximize the number of monitors that I can have on each computer. I will use a GUI library only if I have no other option.
First of all, if an instance is running for a long time and you may require multiple of those running at the same time, you will need to implement multi-threading. In particular, look into concurrency: http://docs.oracle.com/javase/tutorial/essential/concurrency/
Next, once you figure out how to run each instance in a separate thread, you simply access Runtime:
Runtime RT = Runtime.getRuntime();
RT.exec("cmd.exe /c start command", null, new File(newDir)); // for example
There are plenty of GUI libraries that can do what you want. AWT is one see AWTConsoleWindow. Or this one.

How is it possible to stop part of the system and leave the user interface still running?

I am building a system where there is a simple GUI which will trigger the system to execute. I implemented the system in a way that when an exception is thrown, System.exit(0) is called and so the application stops.
I would like the implementation to stop, as it is doing now, however I wish that the GUI would not be closed as well. I tried implementing the system in a separate thread, however when some exception was thrown, the application still closed down.
Is there a way to leave the UI open, but still stop the implementation?
You cannot use Java system.exit for that purpose because that function kills the instance of Java Virtual Machine that is running your code, consequently stopping all the threads of your application.
In order for System.exit to work the way you want it to right now, you would need to have two different processes, so that each of them would run in its own Java Virtual Machine. However, this will make it harder to link things together.
Ideally, you should add some sort of control in the implementation, so that your GUI thread could activate or deactivate a switch which would naturally stop the logic of the implementation. This really is the best way to go in my opinion.
Essentially, they will need to be separate applications. That is, make them separate processes, not separate threads.
This kind of loose-coupling between your UI and back-end will achieve the behaviour you want, and also yields a bunch of other benefits relating to separating the concerns of your UI and your backend.
No, there is no way to keep your UI open if your program is running on the same process. I suppose you could run your UI and your program as separate applications, but that seems tedious and error prone. If an exception is thrown, you should be notifying the user, not just exiting the JVM.

Ensure program runned via Runtime.exec() will die together with the java app

in my java program I am calling external program via Runtime.exec and calling Process.waitFor to wait for its completion. This is some long-running script. I want to ensure that if anything goes wrong with my java app (e.g. gets killed from outside for example, which btw. really happens from time to time in my case) the external running script will die as well.
I had a look on the Runtime.addShutdownHook which might be appropriate for this, but it clearly states that for example on SIGKILL no guarantee can be made whether the shutdownhook will or won't run.
Is there any other way how to ensure an external program runned from Java will die together with the calling java process?
Thanks
If "Runtime.addShutdownHook" is not sufficien I think from the java side you are out of luck. Some ideas:
your script can test if the java app is still running and terminating if needed
let your java app update a file with a timestamp and let your script check periodically if the timestamp is to old (and terminate)
Edit: launch another process which only monitors the java app and the script and kills the script is the java app is gone (of course if THIS process is killed you are out of luck again)

Completely Shut Down Java Application

In my standalone GUI application, I will install a Windows Closed handle on the application
formWindowClosed
within the closed handle, I will perform necessary clean up, and did a final System.exit(0) at the every last code.
This works well, until I receive a complain from customer that the program is not able to shut down properly. My suspect is that, there are still pending I/O operation, which cannot even be interrupted by System.exit(0).
I wish to use Process.destroy() too, to give a final HIT to the application.
However, I am not sure how I can obtain current running process.
You might want to check out this similar question on SO.

Categories