Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default Do not allow to run a program more than once

    Hi,

    I wonder if you know any way to check that a Java program can not run more than once. Let me explain, when the user runs a program for the second time, you get a message saying that it is already running the program.

    Thank you very much.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    When the program is run the first time, create or write to a file or property that the program will check when run the secound time?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Use a ServerSocket. It will only succeed once, a second attempt to bind the same socket will throw an exception (and no race condition).

  4. #4
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Thank you for your quick reply.

    I thought your solution but the problem is that if I create some sort of file properties, the route where you can save this file can change. Or maybe the user can modify the file directly.

  5. #5
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Use a ServerSocket. It will only succeed once, a second attempt to bind the same socket will throw an exception (and no race condition).
    Sorry but I can not understand your choice. You could explain in more detail or give me some examples.

    Thank you very much.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Actually, Java has a Properties class which might be of help.

    Properties (Java Platform SE 6)

    Java: Properties

    Luck,
    CJSL
    Last edited by CJSLMAN; 04-14-2009 at 06:33 PM. Reason: added another link
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by masijade View Post
    Use a ServerSocket. It will only succeed once, a second attempt to bind the same socket will throw an exception (and no race condition).
    It's an easy solution that will work most of the time, but on that rare occasion where some other app (not a duplicate app) is using that socket, it will fail and leave no evidence as to why.

    But you're on the right track. A system resource is needed. It seems like most apps use a lock file.

    If your lock file doesn't exist, you create it. If you succeed in locking it, you are the singleton app and continue running. If you fail to lock it, you exit with a message.

    File locks are automatically cleared by the OS when your process dies, so even if you crash somehow, it will be freed.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Not all file systems support filelocks, and then, if simply using a flag file without a lock, you have a race condition (same scenario with properties). Two apps check, and there is no file, so both attempt to create it (and are liable to both succeed), and you have two running apps. Yes, there could be something else running on the port, but as long as you are intelligent enough to pick an unlikely port, and make which port should be used configurable, I still consider that the best solution.

    Edit: And even with locks, there is a problem. Locks only work as long as everything that might access the locked file "plays nice" (at least on most file systems). So, there is usually nothing to stop anything else from deleting the already locked lockfile and then starting the second app. Sockets are a bit harder to "force" free, and they also are automatically freed when the holding program quits (sometimes with a time wait delay, but then, normally, only when there was some actual traffic on the port, which, in this case, there won't be).

    If you are "happy" with these failings, then by all means, stick with those methods, but the ServerSocket, IMHO, is the most reliable choice.
    Last edited by masijade; 04-15-2009 at 08:32 AM.

  9. #9
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default Solution?

    I tried this. Do you think is a good solution?

    Java Code:
    try {
    			ServerSocket serverSocket = new ServerSocket(666);
    			myCode
    		} catch (IOException ioe) {
    			System.err.println("The program is running yet");
    			System.exit(1);
    		}

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    That is, essentially, how to do it, however, any port under 1024 is a bad choice, as the user running it would have to have adminstrator privilieges (i.e. root on unix), and most of those ports have predefined services. ports from 10,000 and higher are, normally, "random high ports" (i.e. any time the system needs to use a port where no port is predefined, it will take a port randomly from that range). But, since there usually is not that much simulteaneous traffic occurring, you shouldn't have any real problem using one of those, but you can't be sure of that. So, ports from 7000 to 9999 are usually good ports to use, but use a "strange" one, such as 9247, or something, that is not all that likely to be used by something else. Patterns such as 7777, or round numbers such as 7100, are more likely to be used than something like that.

    Edit: I would also suggest explicitly binding to "localhost" (see the other ServerSocket constructors).

  11. #11
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default New changes

    I done some changes...
    What do you think?

    serverSocket = new ServerSocket(8596,0,InetAddress.getByName("127.0.0 .1"));

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Should be good.

  13. #13
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default Terminal Server

    I think we would have a problem if you have multiple users using the same pc (for example Terminal Server).

  14. #14
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Can we ask why you want only one copy running? If you are trying to protect a system resource, can you not use that resource as your semaphore?

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    842
    Rep Power
    6

    Default Runningprogram only once

    1.create some .txt or .dat file

    2.Once the last line of the program is executed then update the created file in step 1 with todays date.

    3.before execution it will check the file created in step 1 whether it is of todays date.

    4.If it is todays date,then it will not execute again or else it will execute.So,the program execute only once per day.

  16. #16
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Why not use a scheduler to just run it once per day? Task Scheduler on Windows or cron on linux
    You could also change the privileges so, on linux for example, only cron can execute it.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  17. #17
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by icsbcn View Post
    I think we would have a problem if you have multiple users using the same pc (for example Terminal Server).
    No, that is what the ServerSocket is for. It can only be bound once, regardless of who is doing it, as long as the application is being executed from the same pc, of course, which it is if multiple users are accessing it over a terminal server, or any other remote access. Now, judging by the posts that have come after this last one of yours, do you want to avoid having it run mutliple times simulteneously, or only once per day. From the previous posts, I am, of course, assuming you want to avoid having multiple instances run in parallel, and are not wanting to restrict to once per day.

  18. #18
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default Reason

    Quote Originally Posted by toadaly View Post
    Can we ask why you want only one copy running? If you are trying to protect a system resource, can you not use that resource as your semaphore?
    Hi,
    The reason for this is that I need ... I just follow orders ... ji ji

  19. #19
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default .txt

    Quote Originally Posted by RamyaSivakanth View Post
    1.create some .txt or .dat file

    2.Once the last line of the program is executed then update the created file in step 1 with todays date.

    3.before execution it will check the file created in step 1 whether it is of todays date.

    4.If it is todays date,then it will not execute again or else it will execute.So,the program execute only once per day.
    In this case, the problem I got in the path where you saved the .txt
    The point of program execution may change, for both the route as well.

  20. #20
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    No, that is what the ServerSocket is for. It can only be bound once, regardless of who is doing it, as long as the application is being executed from the same pc, of course, which it is if multiple users are accessing it over a terminal server, or any other remote access. Now, judging by the posts that have come after this last one of yours, do you want to avoid having it run mutliple times simulteneously, or only once per day. From the previous posts, I am, of course, assuming you want to avoid having multiple instances run in parallel, and are not wanting to restrict to once per day.
    Exactly, I want to run the program many times a day, but only one instance at a time.
    I used ServerSocket. It currently works for me.

Page 1 of 2 12 LastLast

Similar Threads

  1. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •