6

I'd like only a single instance of my Java Swing application to run at a time. If a second instance is opened, I would like it to pass its arguments to the instance already running. How can I do this using a nice, clean Java API? (I'd prefer not to implement it myself using sockets or filesystem locks).

I've seen a number of solutions for enforcing a single instance in Java, but the only one I know of that passes arguments to the running instance uses JNLP's SingleInstanceService, and I'm not writing a Java web start application, so I can't use this (I got a NullPointerException when I tried).

4
  • Duplicate stackoverflow.com/questions/177189/… ?
    – Vlad
    Commented Aug 3, 2011 at 13:55
  • @Vlad There are a lot of questions like this on stackoverflow, but none of them specifically ask how to forward arguments.
    – peskal
    Commented Aug 3, 2011 at 14:01
  • Then I guess you posted two questions in one. Once you decide how to solve problem #1 (single instance) I think you can begin to narrow down solutions for problem #2.
    – Vlad
    Commented Aug 3, 2011 at 14:05
  • @Vlad some APIs, like the one I mentioned, implement both for you. I'm looking for something like that.
    – peskal
    Commented Aug 3, 2011 at 14:07

2 Answers 2

2

See http://www.advancedinstaller.com/user-guide/single-instance-application.html

1

You can use launch4j to do this amongst a whole host of other things:

http://launch4j.sourceforge.net/

4
  • I looked into it and found this example: launch4j.sourceforge.net/docs.html. It doesn't look like there is a way to pass arguments though. Maybe I'm just having trouble finding the right documentation.
    – peskal
    Commented Aug 3, 2011 at 14:02
  • According to their documentation: "Passes command line arguments, also supports constant arguments". I imagine it's transparent, although I haven't tried that yet.
    – Deleted
    Commented Aug 3, 2011 at 14:08
  • 1
    From the features page, right? I think that's unrelated to the single instance part. I couldn't find anything related to passing arguments to other instances when digging through the source a bit
    – peskal
    Commented Aug 3, 2011 at 14:48
  • So you want to pass arguments to an already started instance? If that is the case you're going to have to go outside the JVM and used named pipes or RPC something to deliver a message to the currently running process. There's no way around that even with native tech on windows such as .Net.
    – Deleted
    Commented Aug 3, 2011 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.