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.
Related
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.
I am running my java application by jar file. Now a certain need forces me to check other instance of same application running or not on same Machine. For e.g i must restrict to run only one instance of application to make run, close other application (if running on other tab on terminal). I have searched similar threats from here, but actually unable to figure out.
The easiest way to achieve this would be to create a lock-file when you start your application. When you start a second instance it can check for the existence of such a file and terminate if one is found. When you close the first instance it has to delete this file of course before it finally terminates.
Hi I am writing an SWTBot test which launches a file from a hyperlink using Program.launch. From the bot test I need to check that the 3rd party application opened to display the file. The only thing that seems obvious to me is checking that focus was lost on the underlying workbench but all focus, enabled and visible assertions remain true. Does anyone have any assertion ideas here?
I would recommend to avoid letting the test spawn a process at all. You'll have to wait for the process, kill that process in tear down to leave a clean environment, etc.
If possible I'd rather mock the program launching code during tests and verify that it gets executed if the hyperlink was selected.
But if you really want to test that the 3rd party app was launched , I would rather check if a new process was created. There seems to be neither support in the JRE nor a library to list OS processes so that you probably have to resort to System.exec() ps or tasklist.exe, depending on the OS you are running on.
I created a jar that grabs an image from a URL (webcam of my favorite beach) and saves it to a file using the timestamp as a name. Yes, I know there are programs to do this - I'm a propellerhead and I wanted to do it myself. It works like a champ.
Then I scheduled it to run every minute with CRON. Again, worked like a champ.
* * * * * java -jar /apps/clearcapture.jar
After running for several hours (Mac OS 10.9) I found the machine running at a crawl. Activity Monitor revealed MANY java processes. Without counting them, I would guess that there was one instance for every time the image was captured.
So, what's going on here? I expect that when the jar is done, its parent Java instance would go away.
Thanks,
Chris
At the end of main method of the main class, you should call System.exit to explicitly exit the program. For better exception handling, you should capture all exceptions, log them into a proper log and call System.exit with system error output.
I resolved it by explicitly closing the HTTPUrlConnection at the conclusion of the download.
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.