Topic: 求助,急急急!!!(如何防止多次启动同一Java程序)

  Print this page

1.求助,急急急!!!(如何防止多次启动同一Java程序) Copy to clipboard
Posted by: lypxynok
Posted on: 2003-03-11 15:38

请教,如何控制一个java application 在操作系统中只启动一次

2.Re:求助,急急急!!!! [Re: lypxynok] Copy to clipboard
Posted by: ftang
Posted on: 2003-03-11 21:58

You have to write a shell script.

In unix, you can use ps to find all the processes running, and based on the name of your Java application, see if your Java application is already running. If it is not already running, then you start your Java application. If already running, then quit.

a simpler way is let your java application produces some output, for example, a file, so in your script you just check if this file exists, if the file does not exist, then you start your Java application, otherwise you quit.

3.Re:求助,急急急!!!! [Re: ftang] Copy to clipboard
Posted by: snowbug
Posted on: 2003-03-14 00:34

ftang wrote:
You have to write a shell script.

In unix, you can use ps to find all the processes running, and based on the name of your Java application, see if your Java application is already running. If it is not already running, then you start your Java application. If already running, then quit.

a simpler way is let your java application produces some output, for example, a file, so in your script you just check if this file exists, if the file does not exist, then you start your Java application, otherwise you quit.


As I mentioned in a reply to another post, I think creating an external file is not a good way to handle the problem. I prefer to have a low priority thread fired up when the program starts, and listen to a certain port. When the program is started again, you can try to create a socket connection to this port. And if a connection is successfully created, then you know another instance is already running. The port number can be defined in a property file so it can be easily changed.

The problem with using an external file is: if the program exits abnormally, the file will not be auto-deleted. And then you will need to go into the directory and manually delete that file.

建议使用标题:如何防止多次启动同一Java程序


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923