Allowing Users To Extend Your Java Code – Option 3 Setting Parameters

In my last two articles, I talked about giving developers access to your code and to control its functionality by adding their own code. It is also possible to provide a set of variables which allow the user to control your application. You can then reference these values in your code. There are 3 main ways to do this.

Method ONE – public static variables

If you add a non-final public static variable to your code, it can be set by the user. So you might have the code

public static boolean showMessages=false;

which allows to switch console output on or off.

It is very easy to access but because the variable is static you can only have one value for all your instances.

Method TWO – JVM flags

Java provides a large set of System properties that you can use for Java. For example, I often run Java with the setting


which tells Java to assign 128 megabytes of memory to the JVM. There is a full list of these settings at

If you add a D prefix, you can add your own values. Here is how we tell our PDF viewer to open on page 2.

You can pass in any value but it will need to be treated as a String in your code and converted if you need another object type. You also need to remember that it may be null if not set. And in your code, you do not need a ‘D’ prefix. So here is our code to make use of this value

String page=System.getProperty("");

    int pageNum=-1; //unset value
    }catch(Exception e){
        //log problem with value

These values are passed in when the Java program is run you can have only one setting for each program.

Method THREE – Configuration file

Java makes reading and writing values to an XML file very easy. This allows your program to have an XML configuration file which contains lots of settings. You can provide the user with some configuration menus or just recommend any XML editor and clearly document the format. We use it to provide total configuration of our PDF viewer which allows the user to easily customise the application with no code. Follow this link for some ideas on what you can provide in your code.

In the last three articles I have outlined THREE different ways to give your users access and control over your code in a controlled and useful ways. Do you have any suggestions on using these techniques or alternative suggestions?


Categorized as Java

By Mark Stephens

Mark Stephens runs IDRsolutions, developing the JPedal PDF library in Java, and shares his thoughts on Java, PDF, the Business of Software and Mediaeval History at .

Leave a comment