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

peskal
  • 1,213
  • 1
  • 12
  • 28
  • Duplicate http://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application ? – Vlad Aug 03 '11 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 Aug 03 '11 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 Aug 03 '11 at 14:05
  • @Vlad some APIs, like the one I mentioned, implement both for you. I'm looking for something like that. – peskal Aug 03 '11 at 14:07

2 Answers2

1

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

http://launch4j.sourceforge.net/

Deleted
  • 4,804
  • 1
  • 22
  • 17
  • I looked into it and found this example: http://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 Aug 03 '11 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 Aug 03 '11 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 Aug 03 '11 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 Aug 03 '11 at 14:51