My java appli. will be deployed to Windows client machines. For licensing issues, I want to limit the number of concurrent copies the user can run in their machine. Is there any way to prevent the user from startiing another JVM and run a second copy? For Windows, the user can login as a second user and start all processes isolated from the first user. Is there any system wide resources we can lock regardless of which user is running the process? The only thing I can think of is a file in say c:\. Is there any other more "transparent" resources we can use as a lock?
Traditionally, sockets have been used for this. If your program creates a ServerSocket listening on some port, then no other program will be able to do so; just make your application detect this failure and quit.
I am thinking about this too. But for one thing, firewall will typically alert the user that my java program is trying to listen on a certain port and ask for the user's permission. Would that expose the trick I am using and alllow the user to crack it. Siince as we all know, Java classes can easily be decompiled and hacked.
Another problem which can happen if you use for example a lock file, is that you have to delete the file when the application stops. But when your application is killed forcibly, it might not get the chance to delete the lock file, so that you can't start it up anymore. (You can add a shutdown hook with Runtime.addShutdownHook() but it's not guaranteed to run in all circumstances).