[英]How do I make sure only one instance of my program can be executed?
I want my program, a Java executable .jar, to be run just once. 我希望我的程序,一个Java可执行文件.jar,只运行一次。 I made a program but now I want users not to be able to open more than one instance ....thanks for your time...
我制作了一个程序,但现在我希望用户不能打开多个实例....感谢您的时间......
I've checked the server/client solution and the lock file, but I don't understand them much, I also tried to make them run in NetBeans, with no luck... 我检查了服务器/客户端解决方案和锁定文件,但我对它们了解不多,我还试图让它们在NetBeans中运行,没有运气......
You could use sockets - a ServerSocket can only listen on a port that's not already in use. 您可以使用套接字 - ServerSocket只能侦听尚未使用的端口。 The first launch successfully creates a ServerSocket instance on the port - while that program is running, no other ServerSocket can successfully be created on that port.
第一次启动在端口上成功创建了一个ServerSocket实例 - 当该程序正在运行时,不能在该端口上成功创建其他ServerSocket。
import java.io.IOException;
import java.net.ServerSocket;
public class OneInstance {
private static ServerSocket SERVER_SOCKET;
public static void main(String[] args) {
try {
SERVER_SOCKET = new ServerSocket(1334);
System.out.println("OK to continue running.");
System.out.println("Press any key to exit.");
System.in.read();
} catch (IOException x) {
System.out.println("Another instance already running... exit.");
}
}
}
You can use a lock file solution. 您可以使用锁定文件解决方案。 On startup of the application, have it check for a specific file.
在启动应用程序时,请检查特定文件。 If it doesn't exist, create it and start the application normally.
如果它不存在,请创建它并正常启动应用程序。 If it does exist, exit the application.
如果确实存在,请退出该应用程序。 You need to ensure the file is deleted when the application shuts down (maybe using a FileLock).
您需要确保在应用程序关闭时删除文件(可能使用FileLock)。
You could programmatically extract the jar file, http://www.devx.com/tips/Tip/22124 , the update one file that will prevent the application from running anymore, then rejar it. 您可以通过编程方式提取jar文件, http://www.devx.com/tips/Tip/22124 ,更新一个文件将阻止应用程序再运行,然后重新启动它。
The other option would be to just delete some critical class from the jar file, but neither of these will prevent it from being run again, as they can copy the jar file. 另一种选择是从jar文件中删除一些关键类,但这些都不会阻止它再次运行,因为它们可以复制jar文件。
You can't update a registry as there isn't a platform independent way to do that. 您无法更新注册表,因为没有独立于平台的方法。
Java Web Start can handle this in a platform independent way, but you will need to let your program be started by Java Web Start which requires some tinkering. Java Web Start可以以独立于平台的方式处理这个问题,但是您需要让Java Web Start启动您的程序,这需要一些修改。
See http://download.java.net/jdk7/docs/technotes/guides/javaws/developersguide/examples.html#SingleInstanceService for how to let a single instance handle multiple "start me" requests. 请参阅http://download.java.net/jdk7/docs/technotes/guides/javaws/developersguide/examples.html#SingleInstanceService ,了解如何让单个实例处理多个“启动我”请求。
it also depends what "more than one instance" means. 它还取决于“多个实例”的含义。
Launch4j http://sourceforge.net/projects/launch4j/中有一个选项来限制你的java应用程序的单个实例。这个药水在第四个选项卡中可用,同时为你的java应用程序创建包装器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.