Single instance of a Java desktop application with argument passing
Asked Answered
N

2

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

Nobile answered 3/8, 2011 at 13:50 Comment(4)
Duplicate #177689 ?Underset
@Underset There are a lot of questions like this on stackoverflow, but none of them specifically ask how to forward arguments.Nobile
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.Underset
@Underset some APIs, like the one I mentioned, implement both for you. I'm looking for something like that.Nobile
L
1

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

http://launch4j.sourceforge.net/

Logway answered 3/8, 2011 at 13:53 Comment(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.Nobile
According to their documentation: "Passes command line arguments, also supports constant arguments". I imagine it's transparent, although I haven't tried that yet.Logway
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 bitNobile
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.Logway

© 2022 - 2024 — McMap. All rights reserved.