java.util.Properties, trim, null, whitespace, and escaping

This is the simple test to show the values of properties when loading from a properties file. It tries to answer some common questions about properties value:

Do I need to trim whitespaces from a property value loaded from a properties file?

Yes. Leading whitespaces in a property value are automatically trimmed by Java, but trailing whitespaces are preserved. So if whitespaces are not significant, then property values need to be trimmed val.trim()

Do I need to check null when operating on a property value loaded from a properties file?

Yes. If a property does not exit in the properties file, then calling props.getProperty(key) will return null.

Do I need to check isEmpty when operating on a property value loaded from a properties file?

Yes. If only the key exits in the properties file without assigning any value, then calling props.getProperty(key) will return an empty string. If the value line contains only multiple whitespaces, they will be trimmed away and its value is still empty string.

Do I need to escape = or : characters in property file?

No need to escape = or : in property value, but if the = or : is part of the property key, then you need to escape them. Java treats the first occurrence of = or : or whitespace as the key-value delimiter. So after the delimiter is identified, any use of them no longer needs escaping.

import java.util.*;
import java.io.*;

public class PropertiesTest {
    public static void main(String args[]) throws Exception {
        Properties props = new Properties();
        props.load(new FileInputStream(new File("a.properties")));
        for(String key : props.stringPropertyNames()) {
            System.out.printf("'%s' --> '%s'%n", key, props.getProperty(key));

        System.out.printf("property.not.defined = %s%n", props.getProperty("adsfadsfdjsakfads"));
The properties file a.properties:
no.value =
no.value.with.trailing.spaces =
prop.with.leading.trailing.spaces =      value              
prop.with.leading.spaces =      value
equal.sign.in.value = user=admin
colon.in.value = user:password
equal.sign.colon.in.value = user=admin:password=secrete
equal.sign.in.value.escaped = user\=admin
equal.sign.colon.in.value.escaped = user\=admin\:password\=secrete
\=\:in.key.escaped = value for =:in.key.escaped
=:in.key.not.escaped = value for =:in.key.not.escaped
To compile and run this program:
javac PropertiesTest.java
java PropertiesTest

'equal.sign.in.value' --> 'user=admin'
'equal.sign.colon.in.value' --> 'user=admin:password=secrete'
'no.value' --> ''
'prop.with.leading.spaces' --> 'value'
'equal.sign.in.value.escaped' --> 'user=admin'
'=:in.key.escaped' --> 'value for =:in.key.escaped'
'prop.with.leading.trailing.spaces' --> 'value     '
'colon.in.value' --> 'user:password'
'no.value.with.trailing.spaces' --> ''
'equal.sign.colon.in.value.escaped' --> 'user=admin:password=secrete'
'' --> ':in.key.not.escaped = value for =:in.key.not.escaped'
property.not.defined = null
For complete description, see java.util.Properties javadoc.


NoSuchMethodError: javax.xml.parsers.DocumentBuilderFactory.newInstance

This is the error from running a webapp deployed to appserver:

java.lang.NoSuchMethodError: javax.xml.parsers.DocumentBuilderFactory.newInstance(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljavax/xml/parsers/DocumentBuilderFactory;
The cause: there is an xml-apis.jar under JBOSS_HOME/lib/endorsed directory, and so javax.xml.* classes in xml-apis.jar override those same class from JDK. In particular, DocumentBuilderFactory class in xml-apis.jar is of old version and only has newInstance() method, but not newInstance(String, ClassLoader) method. DocumentBuilderFactory.newInstance(String factoryClassName, ClassLoader) is introduced in Java 6.

The fix is to remove xml-apis.jar from JBOSS_HOME/lib/endorsed directory. XML parser has been in Java proper for a long time, and there is no need for xml-apis.jar in most modern applications.


java.io.IOException: No such file or directory and URI

You need to create a new file with a file URI like file:/tmp/a.txt, but had this error:

java.io.IOException: No such file or directory
 at java.io.UnixFileSystem.createFileExclusively(Native Method)
 at java.io.File.createNewFile(File.java:947)
 at FileTest.main(FileTest.java:7)
The directory /tmp (or java.io.tmpdir) should already exist and should have write permission for the application. So the error message may be complaining the file (a.txt) does not exist. But a.txt is the new file you are trying to create and of course it doesn't exist. Take another look at the source code:
import java.io.File;
import java.net.URI;

public class FileTest {
    public static void main(String args[]) throws Exception {
        File f = new File(args[0]);
        boolean b = f.createNewFile();
        if(b) {
            System.out.printf("Successfully created new file: %s%n", f);
        } else {
            System.out.printf("Failed to create new file: %s%n", f);
Run this class with different input, such as /tmp/a.txt, file:/tmp/a.txt:
$ java FileTest /tmp/a
Successfully created new file: /tmp/a

$ java FileTest /tmp/a
Failed to create new file: /tmp/a
# This failure is expected since /tmp/a already exists.

$ java FileTest file:/tmp/a
Exception in thread "main" java.io.IOException: No such file or directory
So the cause of the IOException is input file URI is not a valid file path. If you have a file URI, then you should use another File constructor to instantiate the File:

//to instantiate a file with URI
//TODO: need to handle java.net.URISyntaxException 
File f = new File(new URI(args[0]));

//to instantiate a filw with path string
File f = new File(args[0]);