Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

Allowing users to extend your Java code – Option 3 Setting parameters

1 min read

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?

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

Enabling SVG Gzip Compression on Apache and NGINX

Gzip compression is a widely supported method of reducing the size of the content sent from a web server in order to improve the...
Leon Atherton
47 sec read

Leave a Reply

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

IDRsolutions Ltd 2019. All rights reserved.