80334

Run one instance of java application [duplicate]

Question:

<blockquote>

<strong>Possible Duplicate:</strong><br /><a href="https://stackoverflow.com/questions/177189/how-to-implement-a-single-instance-java-application" rel="nofollow">How to implement a single instance Java application?</a>

</blockquote>

is there a way to run only one instance of Java application so only I have one process? . is it possible to do it in java?

Answer1:

A simple way to have one instance is to use a service port.

ServerSocket ss = new ServerSocket(MY_PORT);

The benefit of using this approach instead of a locking a file is that you communicate to the instance already running and even check it is working. e.g. if you can't start the server socket use a plain Socket to send it a message like "open a file for me"

Answer2:

The simplest way to do this would be to create a lock file on disk when the application starts if the file does not exist run normally. If the file does exist you can assume another instance of your application is running and exit with a message. Assuming I understood you question correctly.

Answer3:

If you mean "having one instance of your application running" then yes, you could use a lock file to acheive that. When your application starts, create a file and remove it when the program exits. At startup, check if the lock file is there. If the file exists then just exit as another instance of your application is already running.

Answer4:

You could open a socket on startup. If the socket is in use, there is probably already an instance of the app running. A lock file would work, but if your app crashes without deleting the lock file, you'll have to manually delete the file before you can start the app again.

Answer5:

You can apply <strong>Singleton Pattern</strong>

Following are the ways to do it:

<strong>1. Private Constructor and Synchronized method</strong>

public class MyClass{ private static MyClass unique_instance; private MyClass(){ // Initialize the state of the object here } public static synchronized MyClass getInstance(){ if (unique_instance == null){ unique_instance = new MyClass(); } return unique_instance; } }

<strong>2. Private Constructor and initializing the static instance during declaration</strong>

public class MyClass{ private static MyClass unique_instance = new MyClass() ; private MyClass(){ // Initialize the state of the object here } public static MyClass getInstance(){ return unique_instance; } }

<strong>3. Double check Locking</strong>

public class MyClass{ private static MyClass unique_instance; private MyClass(){ // Initialize the state of the object here } public static MyClass getInstance(){ if (unique_instance == null) synchronized(this){ if (unique_instance == null){ unique_instance = new MyClass(); } } return unique_instance; } }

You can also implement a class with <strong>static methods and static variables to apply Singleton pattern, but its not recommended</strong>.

Recommend

  • “Permission denied” while connecting to a page, even with INTERNET permission
  • Reconnection in socket.io problem in `socket.on('message',function(){})`
  • inter thread comunication using ZeroMQ messages [closed]
  • synology php ftp_ssl_connect - Call to undefined function
  • How to send an std::vector of unsigned char over an UDP socket using boost asio?
  • How to read data from socket connection - android
  • Cannot send user message with Spring Websocket
  • How to remove last utf8 char of a python string
  • Firefox extension testing and developing - I'm confused
  • Time taken for Hadoop job to execute
  • Manually Timing out a C# Thread
  • How can I let users share their location in Bot Framework webchat channel?
  • Error in making a socket connection
  • HttpURLConnection Closing IO Streams
  • How to 'create temp table as select' in Slick?
  • Parsing a CSV string while ignoring commas inside the individual columns
  • NHibernate Validation Localization with S#arp Architecture
  • How can I send an e-mail from a vbs script
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Accessing IRQ description array within a module and displaying action names
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Updating server-side rendering client-side
  • Can I make an Android app that runs a web view in Chrome 39?
  • How to pass list parameters for each object using Spring MVC?
  • Matrix multiplication with MKL
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • Getting Messege Twice Using IMvxMessenger
  • Android Google Maps API OnLocationChanged only called once
  • File not found error Google Drive API
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • sending mail using smtp is too slow
  • Converting MP3 duration time
  • How can I use threading to 'tick' a timer to be accessed by other threads?