Java: How to terminate the process created with ProcessBuilder("startx", "firefox") - java

How do I terminate the process I created with ProcessBuilder("startx", "firefox")? process.destroy() and process.destroyForcibly() don't work.
I'm starting a firefox kiosk, but only once the rest of the Java application is up and running (so that I can keep quick boot times and not have to wait for X to start). Is there a better way to do this than "startx firefox"? Is there a way of passing something to x to get it to close.
I need to restart the software to be able to update it. Obviously I'd rather not have to reboot the machine just to update it.

Related

How can I figure out why my Java application keeps on running after exit?

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/

Java Process running from Windows Powershell sometimes randomly stops until pressing Ctr+C

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.

Make program sleep until a windows task has completed

I have a small program that will backup some important files then uninstall a program. once that is done it will then install a bunch of new stuff and backup the old files and configure everything. It works fine but the problem is when i run the program it just loads up a bunch of installers and restores the backup files before the programs have even installed.
I know this is happening because the java isnt responsible for the install of the programs its windows job but is there a way to delay the program so that it wont go onto the next step until the last 1 has completed? i could guess roughly how long it takes to complete a step and use thread.sleep() but i would like something more accurate. Is there a way to somehow find out from windows when it has finished doing a task?
If you start the installer using the ProcessBuilder then you get a Process object returned when you call start().
If you then call waitfor() on the Process your thread will wait until the installer is finished.

Prevent a user from terminating a java program?

I want to install a monitoring system on a computer (the program is a jar file) and run it on start up every time any user logs on. However, I don't want the user to be able to terminate it since then it won't be able to be monitored any longer.
We have tried several ways:
Installing it as a service - the problem here is that our program doesn't work any longer; it can't connect to the computer that's monitoring it. We used "Yet Another Java Service Wrapper" for this, and looked into some other wrappers as well that could help us install it as a service.
Running the program on start up (using the folder startup), but not giving the basic user the privileges to edit/delete/mess around with the files. However, this seems to slow the whole computer down? This doesn't happen when we run the bat file executing the jar directly. Another issue with this is that the user can just go to the task manager and kill the java process.
We tried a variation of the previous one to solve the issue of the process being killed, by having another process. One will spawn the other and these 2 processes will keep tabs on each other. If one terminates, the other detects it and runs it to start it up again. Although it can have issues if the user is fast enough in killing both processes before either is respawned again, the bigger issue is that it sometimes has problems with connecting to other computers. We didn't have this problem when it was just 1 jar.
Does anyone have any idea on how these problems can be solved?
The context here is windows, but if you have suggestions for linux and mac that would be nice too!
Way to go is to run the program as a service. You should investigate any trouble between your application and your system's firewall. If you have windows firewall activated, you should add an exception for java.exe or javaw.exe.
In order to give elevated privileges to your program, you can set the service to run as another user. You can do this from the "Log on" tab in the service properties.
You'll want to have the program started under a user with elevated permissions. On WIndows this would the the Administrator, linux would use root. On Windows, its likely that you will need to start it as a service. But I really don't know why that would hinder the network communications.

Using an empty file to have one application instance

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.

Categories

Resources