How to find my class is running in the same machine or not

Hi,

Can any one help me, I have a class called TestClass it has a main method. I want to restrict this class, to run only one occurrence in my system. i.e. If I open 2 command prompts and If I run this class in 2 command prompts, in one command prompt it should run, in another command prompt it should display msg like "This class is already running in the system".

Thanks in advance.

[405 byte] By [suman_tejaa] at [2007-10-3 3:41:26]
# 1
Try to open a java.net.ServerSocket on a fixed port. If it succeeds, you are alone. If not, not.Try to use something that isn't a well-known port.
ejpa at 2007-7-14 21:37:15 > top of Java-index,Core,Core APIs...
# 2
You could simply create a text file, if that file does not already exist. If itdoes exist, you know you are already running. Just remember to delete thefile on exit of the original running instance.-Cludge
cludgea at 2007-7-14 21:37:15 > top of Java-index,Core,Core APIs...
# 3

> You could simply create a text file, if that file

> does not already exist. If it

> does exist, you know you are already running. Just

> remember to delete the

> file on exit of the original running instance.

This is not as reliable as the previously mentioned server socket approach. With this approach, if your program crashes, it may not delete the file. With the socket approach, even if the program crashes, the socket will be released.

jverda at 2007-7-14 21:37:15 > top of Java-index,Core,Core APIs...
# 4
Also, someone or something else could delete the file.
ejpa at 2007-7-14 21:37:15 > top of Java-index,Core,Core APIs...