user1381151
user1381151

Reputation: 63

Count No of times Program has been executed

How can I get the number of times a program has previously run in java without keeping a file and tallying. Is there a Application class or something in java to check the count

Upvotes: 5

Views: 2747

Answers (2)

Rahul Thakur
Rahul Thakur

Reputation: 932

You can do something like:

public class Test {

    private static int INSTANCE_COUNTER = 0;

    public Test() {

        INSTANCE_COUNTER++;
    }

    public static final int getInstanceCounter() {

        return INSTANCE_COUNTER;
    }

    //-Methods-//
}

Now, you can call Test.getInstanceCounter() to get the number.

Upvotes: 0

Greg Kopff
Greg Kopff

Reputation: 16555

You could use the preferences API. This eliminates the need for you to do the file i/o. You will still need to read the preference value, increment it, and then store the new value.

On Windows, the preferences API uses the registry. On Unix, the preferences API uses the file system as the backing store (but this is hidden from you as a developer).

You can get/store preferences for either the system, or for an individual user. (It's not clear if you want to know how many times a user ran the program, or how many times anyone on said computer ran the program). Look for Preferences.systemNodeForPackage() and Preferences.userNodeForPackage() in the documentation.

Upvotes: 6

Related Questions