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/… ? 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. 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. 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. Commented Aug 3, 2011 at 14:07

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

http://launch4j.sourceforge.net/

4 Comments

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.
According to their documentation: "Passes command line arguments, also supports constant arguments". I imagine it's transparent, although I haven't tried that yet.
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
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.