Deploying a Single Java Application

I am not sure if this is a Windows or Java issue.

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

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

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

If this is the case, I assume that I need to do something like this: When the second instance of the application starts, it checks if another instance is running (how?), Opens a link for the first instance, and passes the file name. Right?

+4
source share
5 answers

Here is one approach:

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

If the port is already open, it means that another instance is working, so open the socket for the instance listening port that is already running and pass enough information to it to complete the processing, and then exit.

+2
source

Here is the Java pseudo 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, ie exit if no service is running. 
+2
source

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

+2
source

See the JWS solution . This uses a SingleInstanceService . JWS may also declare interest in the file type. See the demo file for files for more information.

It will run on Windows * nix and OS X (and provides many other useful deployment features).

+1
source

You can use the JUnique library. It provides support for running a single instance Java application and is open source.

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

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

0
source

Source: https://habr.com/ru/post/1399384/


All Articles