Unexpected exception calling VelocityEngine.mergeTemplate when using 2.4 or 2.4.1

"Kish, Robert [AG]" <[email protected]> Tue, 10 Jun 2025 21:50:07 +0000
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <SA1PR09MB962394953915C57D6F59A54EAD6AA@SA1PR09MB9623.namprd09.prod.outlook.com>
--_000_SA1PR09MB962394953915C57D6F59A54EAD6AASA1PR09MB9623namp_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hello!

We have 2 similar environments and under Velocity Engine 2.3, the code work=
s as expected and we get back the template from the API call.
But when upgrading to 2.4.1 (or trying 2.4) one of our environments fails w=
ith an exception. Unfortunately, it's in an environment where I can't use a=
 debugger.

I'm looking for ideas on how to resolve this issue or identify a specific b=
ug that can be reported to the Velocity Team for resolution.

Log + Exception
o.a.v.macro - autoload off: VM system will not automatically reload global =
library macros
o.a.v.parser - XXXXXXXXXXXXXXXXXXXXXXXXXXXX: offset -1, count 79, length 40=
96
java.lang.StringIndexOutOfBoundsException: offset -1, count 79, length 4096
        at java.base/java.lang.String.checkBoundsOffCount(String.java:3304)=
 ~[?:?]
        at java.base/java.lang.String.rangeCheck(String.java:280) ~[?:?]
        at java.base/java.lang.String.<init>(String.java:276) ~[?:?]
               at org.apache.velocity.runtime.parser.VelocityCharStream.Get=
Image(VelocityCharStream.java:434) ~[velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.runtime.parser.StandardParserTokenManager.jj=
FillToken(StandardParserTokenManager.java:7796) ~[velocity-engine-core-2.4.=
jar:2.4]
        at org.apache.velocity.runtime.parser.StandardParserTokenManager.ge=
tNextToken(StandardParserTokenManager.java:7866) ~[velocity-engine-core-2.4=
.jar:2.4]
        at org.apache.velocity.runtime.parser.StandardParser.jj_scan_token(=
StandardParser.java:5611) ~[velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.runtime.parser.StandardParser.jj_3R_EndingZe=
roWidthWhitespace_1591_5_49(StandardParser.java:5448) ~[velocity-engine-cor=
e-2.4.jar:2.4]
        at org.apache.velocity.runtime.parser.StandardParser.jj_3_3(Standar=
dParser.java:5454) ~[velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.runtime.parser.StandardParser.jj_2_3(Standar=
dParser.java:3886) ~[velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.runtime.parser.StandardParser.Statement(Stan=
dardParser.java:654) ~[velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.runtime.parser.StandardParser.process(Standa=
rdParser.java:495) ~[velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.runtime.parser.StandardParser.parse(Standard=
Parser.java:184) [velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.runtime.RuntimeInstance.parse(RuntimeInstanc=
e.java:1344) [velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.Template.process(Template.java:145) [velocit=
y-engine-core-2.4.jar:2.4]
        at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadRes=
ource(ResourceManagerImpl.java:429) [velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.runtime.resource.ResourceManagerImpl.getReso=
urce(ResourceManagerImpl.java:346) [velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeI=
nstance.java:1694) [velocity-engine-core-2.4.jar:2.4]
        at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEng=
ine.java:284) [velocity-engine-core-2.4.jar:2.4]

Environment that throws the exception:
Linux / Tomcat

Server version name:   Apache Tomcat/9.0.104
Server built:          Apr 4 2025 12:32:55 UTC
Server version number: 9.0.104.0
OS Name:               Linux
OS Version:            4.14.355-277.647.amzn2.x86_64
Architecture:          amd64
Java Home:             /usr/lib/jvm/java-11-amazon-corretto.x86_64
JVM Version:           11.0.27+6-LTS
JVM Vendor:            Amazon.com Inc.


Environment that does not throw the exception:
Windows / TomEE

INFO: Server version name:   Apache Tomcat (TomEE)/9.0.82 (8.0.16)
INFO: Server built:          Oct 11 2023 13:15:08 UTC
INFO: Server version number: 9.0.82.0
INFO: OS Name:               Windows 11
INFO: OS Version:            10.0
INFO: Architecture:          amd64
INFO: Java Home:             C:\Program Files\Amazon Corretto\jdk11.0.26_4
INFO: JVM Version:           11.0.26+4-LTS
INFO: JVM Vendor:            Amazon.com Inc.

Robert Kish (Consultant) | Application Developer, IT Application Services |=
 NJ Dept. of Agriculture

CONFIDENTIALITY NOTICE: The information contained in this communication fro=
m New Jersey Department of Agriculture is privileged and confidential and i=
s intended for the sole use of the persons or entities who are the addresse=
es. If you are not an intended recipient of this email, the dissemination, =
distribution, copying or use of the information it contains is strictly pro=
hibited. If you have received this communication in error, please immediate=
ly contact the sender of this email message to arrange for the return of th=
is information.

--_000_SA1PR09MB962394953915C57D6F59A54EAD6AASA1PR09MB9623namp_--