Setting up system properties

This is a simple and easy way to add more depth in your program by setting up custom system properties. Although the aim in this example isn’t read properties from file, the aim is to set it up in working environment. This tutorial is done by using Eclipse IDE.

Theory behind

Sometimes, for reasons unknown, you might have to set up system or maybe environment variables in your working system. This could be because you would like to use different files in different system or maybe your development uses different properties file than production.

There are many ways to set this up, for example you could use class that does these before other classes or you could set up custom properties in your system. But not only you are able to set properties you are also able to set up enviroment variables. In windows for example, you could set up custom environment variables by going system -> advanced system settings -> enviroment variables. In Linux you could do export in .bashrc and add the value you desire to system. And moreover you could do these also in Tomcat configuration (both in server or in IDE enviroment) or in the eclipse VM lines.

In order to add Tomcat server variables to Linux based system, you are going to need to add new file in config folder. This file name is setenv.sh. You can find tutorial for it here. In windows server, type in search tomcat configuration and then go to arguments and add line.

As you can see, these are only couple examples how it could be done.

Example in VM

First let’s create class and then let’s print what we already have in system. Here is the method for it

public static void printAllSystemVariables(){
     Properties properties = System.getProperties();
     Iterator it = properties.keySet().iterator();
     while(it.hasNext()){
         String key = (String) it.next().toString();
         System.out.println(key + " = " + System.getProperty(key));
         }
     }

There are other ways also to print this beauty, so keep looking for other styles. For the tip, you could also use Enumeration keys in while loop.

As you can see there is already lot of in it. But how we set up our very own unique value?

In Eclipse, you are able to do this by selecting the class right click and then choose run as -> run configuration. Then we are prompted and there is new window with lot of stuff. Here we choose tab Arguments.

Then into the VM arguments we type:
-Dcustom=”Custom variable”

argument_variable

Eclipse VM argument

Then print list again and look for variable name custom. You are also able to set your own variables in code also. This can be done, for example:

public static void setSystemProperty(){
        String variableValue = "Variable value";
        System.setProperty("Custom variable", variableValue);
 }
 
 
 public static void main(String[] args){
       setSystemProperty();
       System.out.println(System.getProperty("Custom variable"));
 }

Of course this might not be what we wan’t but we are now on track. Next step is to get this into the running service.

Tomcat

Depending are you running Linux or Windows server, basically you done same thing but way differently.

In order to add Tomcat server variables to Linux based system, you are going to need to add new file in config folder. This file name is setenv.sh. You can find tutorial for it here. In windows server, type in search tomcat configuration and then go to arguments and add line like -DcustomVariable=variable.

In Eclipse, you are able to test this by double clicking your tomcat server and then press link open lauch configuration. Now like in Eclipse VM, add argument same way and the start your server. In order to see the effects, you need to run project in that tomcat you have configured in Eclipse.

 

– Tuomas Törmä

Leave a comment