Update: JPDF2HTM5 has been rebranded as BuildVu and JPDFForms has been rebranded as FormVu

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

-Xmx128M

which tells Java to assign 128 megabytes of memory to the JVM. There is a full list of these settings at http://blogs.sun.com/watt/resource/jvm-options-list.html

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.

-Dorg.jpedal.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("org.jpedal.page");
if(page!=null){

    int pageNum=-1; //unset value
    try{
        pageNum=Integer.parseInt(page);
    }catch(Exception e){
        //log problem with value
        pageNum=-1;
    }

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?

Related Posts:

The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>