简体   繁体   中英

how to prevent multiple clicks on an executable jar

I have an executable jar that runs a Java Swing application with an internal SqlLite db. Users (by mistake) do more than a click on the jar, causing the db lock. I'd like to prevent this behavior. What can I do? thank you very much

You need some kind of synchronization mechanism.

Either you need to code it yourself, or you can create a Java WebStart configuration for your application, where Java WebStart can handle the "only one invocation" through the Single Instance Service (which you must call explicitly in your code).

See http://docs.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/examples.html#SingleInstanceService for an example.

The first instances accessing the db should acquire a lock of some sort on the db and all further instances should first check if there is already such a lock. If there is one -> "I am not the first, show warning/error, quit.", if there is none "I am the first, get a lock, proceed."

You can use JPS or JNI (need to implement on different platform). The attached is the JPS code to check the Java application instance. You can modify it to more OO.

Using File, Socket or Registry as a lock is not perfect, since there are a lot of chance that a mis-operation can make your application can not start any more (for example, another program occupe the same port)


import java.io.*;

public class TestRun {

      public TestRun() {}



      public static void main(String args[]) {



            String jpsApp = "jps -mlvV";
            int count = 0;
            try {

                  Process p = Runtime.getRuntime().exec(jpsApp);
                  //parser the result to check if TestAPP is running

                  InputStream is = p.getInputStream();

                  BufferedReader reader = new BufferedReader(new InputStreamReader(is));

                  String line = null;

                  while ((line = reader.readLine()) != null) {

                        System.out.println();

                        System.out.println(line);

                        String[] pair = line.split(" ");

                        if (pair.length >= 2) {

                              System.out.println("name is " + pair[1]);

                              if (pair[1].trim().indexOf("TestRun") > -1) {

                                    count++;

                                    System.out.println("count is " + count);

                              }

                        }

                  }
                  //it is running, just exit the second instance

                  if(count>1){


                        System.out.println("Has run a application!");
                        return;

                  } 

            } catch (Exception ex) {

                  ex.printStackTrace();

            }


      }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM