简体   繁体   English

如何确保只能执行我的程序的一个实例?

[英]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. 它还取决于“多个实例”的含义。

  1. once per machine -> tcp / mailslot / atom / fixed path 每台机器一次 - > tcp / mailslot / atom / fixed path
  2. once per user -> lock file in user homedir 每个用户一次 - >用户homedir中的锁定文件
  3. once user session -> a little more complicated and more dependent on platform 一旦用户会话 - >更复杂一点,更依赖于平台

Launch4j http://sourceforge.net/projects/launch4j/中有一个选项来限制你的java应用程序的单个实例。这个药水在第四个选项卡中可用,同时为你的java应用程序创建包装器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保仅初始化一个实例,但从另一个类初始化? - How do I make sure to initialize only one Instance but from another class? 如何确保一次只调用一个servlet(不是实例)? - how to I make sure that only one servlet(not instance) is call at a time? 如何确保在 Java 中只选择了一个 JRadioButton? - How Do I Make Sure Only One JRadioButton Is Selected In Java? 如何确保仅启动一个线程 - How do I make sure only one thread gets started 如何确保if语句内的指令(也位于for循环内)仅执行一次? - How do I make sure the instruction inside an if statement, which is also inside a for loop, gets executed only once? 如何确保仅一个指定声音的实例在循环,如何停止循环? - How do I make sure only 1 instance of a specified Sound is looping and how do I stop it from looping? 我们如何确保即使在2个不同的JVM中也只运行任何应用程序的一个实例 - How can we make sure that only one instance of any application in running even in 2 different JVM's 如何确保仅使用我的origninal客户端向我的服务器发出请求? - How do I make sure only my origninal client is used to make requests to my server? 如何确保我的 tensorFlow Java 程序在 Windows 上使用我的 GPU? - How can I make sure that my tensorFlow Java program is using my GPU on Windows? 如何确保该方法只执行一次并且仅从一个线程执行? - How to make sure that method is executed only once and from one thread only?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM