java/shellscript code to find out if a jar file is already running on current machine

前端 未结 2 1847
-上瘾入骨i
-上瘾入骨i 2021-01-03 02:44

How do I find out if a specific JAR file is running on my machine or not? I just want to ensure that a specific JAR is only executed once at any time-- so if another request

2条回答
  •  囚心锁ツ
    2021-01-03 03:17

    Try to create a new jar,

    create a class inside with like this (not yet functional code, just a scribble):

    static ServerSocket unicorn;
    public void load(){
        unicorn=new ServerSocket(39483); // whatever-port
    
        URLClassLoader myloader = new URLClassLoader(new URL[]{this.getClass().getResource("/META-INF/specific.jar")});
        ... // do all your unique stuff
        Runtime.addShutdownHook(new Runnable(){unicorn.close();})
    }
    

    Place your specific.jar inside the new.jar. If ever another instance of this jar try to be load, a exception will be thrown: Socket already in use.

提交回复
热议问题