Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I believe this <a href="http://www.rbgrn.net/blog/2008/05/java-single-application-instance.html" rel="nofollow noreferrer">article</a>, by :</p> <blockquote> <p>having the first instance attempt to open a listening socket on the localhost interface. If it's able to open the socket, it is assumed that this is the first instance of the application to be launched. If not, the assumption is that an instance of this application is already running. The new instance must notify the existing instance that a launch was attempted, then exit. The existing instance takes over after receiving the notification and fires an event to the listener that handles the action.</p> </blockquote> <p>Note: <a href="https://stackoverflow.com/users/4427/ahe">Ahe</a> mentions in the comment that using <code>InetAddress.getLocalHost()</code> can be tricky:</p> <blockquote> <ul> <li>it does not work as expected in DHCP-environment because address returned depends on whether the computer has network access.<br> Solution was to open connection with <strong><code>InetAddress.getByAddress(new byte[] {127, 0, 0, 1})</code></strong>;<br> Probably related to <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4435662" rel="nofollow noreferrer">bug 4435662</a>.</li> </ul> </blockquote> <ul> <li>I also found <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037" rel="nofollow noreferrer">bug 4665037</a> which reports than Expected results of <code>getLocalHost</code>: return IP address of machine, vs. Actual results : return <code>127.0.0.1</code>. </li> </ul> <blockquote> <p>it is surprising to have <code>getLocalHost</code> return <code>127.0.0.1</code> on Linux but not on windows.</p> </blockquote> <hr> <p>Or you may use <a href="http://web.archive.org/web/20090529082521/http://www.trap17.com/index.php/how-implement-single-instance-application-java_t59760.html" rel="nofollow noreferrer"><code>ManagementFactory</code></a> object. As explained <a href="http://web.archive.org/web/20081203154345/http://www.trap17.com/forums/how-implement-single-instance-application-java-t59760.html" rel="nofollow noreferrer">here</a>:</p> <blockquote> <p>The <code>getMonitoredVMs(int processPid)</code> method receives as parameter the current application PID, and catch the application name that is called from command line, for example, the application was started from <code>c:\java\app\test.jar</code> path, then the value variable is "<code>c:\\java\\app\\test.jar</code>". This way, we will catch just application name on the line 17 of the code below.<br> After that, we search JVM for another process with the same name, if we found it and the application PID is different, it means that is the second application instance.</p> </blockquote> <p>JNLP offers also a <a href="http://forums.sun.com/thread.jspa?messageID=9509621" rel="nofollow noreferrer"><code>SingleInstanceListener</code></a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload