2

I want my program, a Java executable .jar, to be run just once. 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...

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
HoNgOuRu
  • 239
  • 3
  • 6
  • 12

6 Answers6

8

You could use sockets - a ServerSocket can only listen on a port that's not already in use. 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.

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.");
        }
    }
} 
Nate
  • 16,748
  • 5
  • 45
  • 59
7

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).

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • Thanks I think I'll stick with your solution. – HoNgOuRu Sep 27 '09 at 00:41
  • Lock files are the standard way to go and does to create hassles with firewalls etc. – whatnick Sep 27 '09 at 03:28
  • Also remember if the application crashes and does not exit cleanly it can't be ran again as long as the lockfile is in place. The user needs to be made aware of the location of the lockfile so that they can fix it by hand. – whatnick Sep 27 '09 at 03:30
  • Yep this is quite standard and we use this a lot. – Shaun Sep 27 '09 at 03:55
  • To add to my original answer, you don't need to actually delete the file. You can just release the lock on it. The application just needs to recreate the lock file in case it was accidentally deleted. – Jeff Storey Sep 27 '09 at 14:03
0

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.

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.

You can't update a registry as there isn't a platform independent way to do that.

James Black
  • 41,583
  • 10
  • 86
  • 166
0

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.

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.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Apparently the notes were taken offline. There seems to be a mirror at http://www.jdk7.ru/technotes/guides/javaws/developersguide/examples.html#SingleInstanceService – Thorbjørn Ravn Andersen Nov 24 '11 at 12:43
0

it also depends what "more than one instance" means.

  1. once per machine -> tcp / mailslot / atom / fixed path
  2. once per user -> lock file in user homedir
  3. once user session -> a little more complicated and more dependent on platform
0

There is an option in Launch4j http://sourceforge.net/projects/launch4j/ to restrict single instance of your java application.This potion is available in the fourth tab while creating wrapper for your java application.

Prabhu
  • 1