4

I'm not sure if the is a Windows or a Java question.

I have a Java application running under Windows. I have associated a particular file extension (say, .xyz) with my application, so that when I double click a .xyz file, my Java application is started and the main() method sees the .xyz file as its first argument.

But when I click another .xyz file, a new instance of the Java application is launched. I would prefer the existing application to handle the new file.

Is there a standard way to do this, or do I need to program it from scratch?

If the latter is the case, I presume I have to do something like this: When the second instance of the application is launched, it checks if another instance is already running (how?), opens a communication link to that first instance, and passes the name of the file. Right?

haylem
  • 22,460
  • 3
  • 67
  • 96
oz1cz
  • 5,504
  • 6
  • 38
  • 58
  • Exact duplicate of [How to implement a single instance Java application?](http://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application) – haylem Mar 02 '12 at 13:13
  • 1
    Does this answer your question? [How to implement a single instance Java application?](https://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application) – Alexander Biryukov Nov 07 '20 at 00:24

5 Answers5

2

Here is the pseudo Java code.

try {
    // start as non daemon service.
    new FileNameListener(new ServerSocket(KNOWN_PORT)).start();

} catch (BindException alreadyRunning) {
    // don't start a new service
}
// send to service, which could be the one just started.
sendFileName(new Socket("localhost", KNOWN_PORT), filename);

// finish the current thread, i.e. exit if no service is running.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

Here's one approach:

When your main() fires, it first opens a particular port and listens for other (subsequent) open-file actions.

If the port is already open, it means there's another instance running, so open a socket to the already-running instance's listening port and hand it enough information to complete processing then exit.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    The problem with this and other solutions that rely on listening on a socket is that the first time the application runs, the Windows firewall asks for confirmation that the program (in this case the JVM) is allowed to listen for connections. Maybe that's just something I'll have to live with, but a firewall warning like that tends to scare novice users. – oz1cz Mar 02 '12 at 14:05
  • I discovered that by using `ServerSocket(int port, int backlog, InetAddress bindAddr)` rather than `ServerSocket(int port)` and providing 127.0.0.1 for `bindAddr` I can avoid the Window firewall complaint. – oz1cz Mar 02 '12 at 14:19
  • I'm accepting this answer. Apologies and thanks to everyone else who provided similar answers. I believe I can only accept one answer. – oz1cz Mar 02 '12 at 14:20
2

Also, read the answers to this exact duplicate of your question:

Community
  • 1
  • 1
haylem
  • 22,460
  • 3
  • 67
  • 96
1

See the JWS solution. That uses the SingleInstanceService. JWS can also declare an interest in a file-type. See the file service demo. for more info.

It will work on Windows, *nix & OS X (and provides a lot of other nice deployment features).

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

You can use JUnique library. It provides support for running single-instance java application and is open-source.

http://www.sauronsoftware.it/projects/junique/

See also my full answer at How to implement a single instance Java application?

Community
  • 1
  • 1
kolobok
  • 3,835
  • 3
  • 38
  • 54