14

How do I escape the hash sign (#) in Java properties files.

We have an internationalization framework that uses Java properties files.

There is a column called number and we want its header to be #. This ...

number=#

... doesn't work.

Trinimon
  • 13,839
  • 9
  • 44
  • 60
flybywire
  • 261,858
  • 191
  • 397
  • 503
  • 1
    Since it is java, you need to replace hash(`#`) with `\\#` in your code. Notice the double slashes. Alternately you can replace `#` with `\\u0023`. – Sabin May 19 '14 at 20:12

4 Answers4

8

This one should work without any escape character:

number=#

Just made a small test using code ....

prop.load(new FileInputStream ("./res/app.properties"));
System.out.println("Property: " +prop.getProperty("Number"));

... and property file (note upper/lower case):

Number=#
Text=test

Result:

Property: #

So rather check your spelling, lower/upper case or further processing.

Check out this or that one (linking Java documentation too) for more.

p.s.: though it shouldn't be required you could also try the Unicode sequence: \u0023.

Cheers!

Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60
0

As the default escape character in Java is the '\' try to escape the hash with it.

DmiN
  • 736
  • 1
  • 7
  • 21
0

I found this question when looking how to escape properly in the following case (DOES NOT WORK):

zendesk.ticket.subject.prefix=#{{ticket.id}}

In this case it needs escaping, otherwise the following exception occurs:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'createTicketService' defined in file [/Users/***]: Unsatisfied dependency expressed through constructor parameter 6; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'ticket' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 64 common frames omitted
Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'ticket' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:164)
    at org.springframework.beans.factory.support.AbstractBeanFactory.evaluateBeanDefinitionString(AbstractBeanFactory.java:1448)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1088)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 78 common frames omitted
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'ticket' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:226)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:51)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
    at org.springframework.expression.spel.ast.InlineList.getValueInternal(InlineList.java:95)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:131)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:297)
    at org.springframework.expression.common.CompositeStringExpression.getValue(CompositeStringExpression.java:105)
    at org.springframework.expression.common.CompositeStringExpression.getValue(CompositeStringExpression.java:42)
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161)
    ... 83 common frames omitted

The / a solution would be to use #{'#'} the end result being (WORKS):

zendesk.ticket.subject.prefix=#{'#'}{{ticket.id}}

Clijsters
  • 4,031
  • 1
  • 27
  • 37
0

Hash (#) is escaped with the \ character in properties files. For example, to map key #foo to value #bar, you write:

\#foo=\#bar

It is applicable on both keys and values, although (as hinted in other answer) it is not strictly required for values.

holmis83
  • 15,922
  • 5
  • 82
  • 83