RFR: 8341641: Make %APPDATA% and %LOCALAPPDATA% env variables available in *.cfg files

Alexey Semenyuk asemenyuk at openjdk.org
Thu Mar 6 01:08:46 UTC 2025


jpackage app laucnher will expand environment variables in .cfg files.

Previously jpackage app launcher only replaced `$APPDIR`, `$BINDIR`, and `$ROOTDIR` tokens with the corresponding path values. With this patch, any environment variable can be expanded. The syntax is shell-like `$ENV_VAR_NAME` or `${ENV_VAR_NAME}`. If `$ENV_VAR_NAME` syntax is used, the variable name stops at the first character outside of `[a-zA-Z0-9_]` character range. If `${ENV_VAR_NAME}` syntax is used, the variable name stops at the first `}` character after `${` substring. E.g:
| String    | Variables | Variable Values | Expanded string | Note |
| -------- | ------- |------- |------- |------- |
| <pre>Welcome $USER!</pre>  | <pre>USER</pre> | <pre>USER=John</pre> | <pre>Welcome John!</pre> ||
| <pre>Welcome $USER!</pre>  | <pre>USER</pre> | <pre>not set</pre> | <pre>Welcome $USER!</pre> | Unset variables are not expanded. |
| <pre>Welcome $USER2</pre> | <pre>USER2</pre>   | <pre>USER2=John</pre> | <pre>Welcome John!</pre> ||
| <pre>Welcome ${USER}2</pre> | <pre>USER</pre>  | <pre>USER=John</pre> | <pre>Welcome John2!</pre> ||
| <pre>Welcome $USER-2</pre> | <pre>USER</pre>   | <pre>USER=John</pre> | <pre>Welcome John-2!</pre> ||
| <pre>Welcome ${USER-2}</pre> | <pre>USER-2</pre>   | <pre>USER-2=John</pre> | <pre>Welcome John!</pre> | `USER-2` is likely to be an invalid env variable name, but jpackage launcher is not validating names. |

`$` character combination prevents variable expansion:
| String    | Variables | Variable Values | Expanded string |
| -------- | ------- |------- |------- |
| <pre>Hello \$A! Bye $B</pre>  | B | <pre>B=John</pre> | <pre>Hello $A! Bye John</pre> |
| <pre>Hello \${A}! Bye $B</pre>  | B | <pre>B=John</pre> | <pre>Hello ${A}! Bye John</pre> |

`\` character combination escapes ``:
| String    | Variables | Variable Values | Expanded string |
| -------- | ------- |------- |------- |
| <pre>Hello \\\$A! Bye $B</pre>  | A, B | <pre>A=Ana</pre><pre>B=John</pre> | <pre>Hello \\Ana! Bye John</pre> |

If `` character is not followed by another `` character or `$` character, it is interpreted as a regular character:
| String    | Expanded string |
| -------- | ------- |
|<pre>a\\b\\c</pre>|<pre>a\\b\\c</pre>|


Expansion is non-recursive:
| String    | Variables | Variable Values | Expanded string | Note |
| -------- | ------- |------- |------- |------- |
| <pre>Hello $A!</pre>  | A | <pre>A=$B</pre><pre>B=John</pre> | <pre>Hello $B</pre> | Variable "B" will not be expanded |

Values of `APPDIR`, `BINDIR`, and `ROOTDIR` environment variables are ignored, and these names are substituted by values calculated by jpackage app launcher as previously.

`$APPDIR` is equivalent to `${APPDIR}`.
`$BINDIR` is equivalent to `${BINDIR}`.
`$ROOTDIR` is equivalent to `${ROOTDIR}`.

You will find two entities dealing with token replacement in this patch:
 - StringProcessing.cpp/.h - code that performs variable substitution in .cfg files;
 - TokenReplace.java - helper class for jpackage tests. It will be later used with other jpackage tests and to implement [jdk.jpackage.internal.OverridableResource.substitute()](https://github.com/openjdk/jdk/blob/11a37c829c12d064874416a7b242596cf23972e5/src/jdk.jpackage/share/classes/jdk/jpackage/internal/OverridableResource.java#L305) method.

-------------

Commit messages:
 - Shorter test desc
 - Minor
 - A bit of javadoc
 - Update copyright year
 - Applied bin/blessed-modifier-order.sh
 - 8341641: Make %APPDATA% and %LOCALAPPDATA% env variables available in *.cfg files
 - Moved $APPDIR, $BINDIR, and $ROOTDIR macros to JPackageCommand
 - Add Executor.setEnvVar()
 - Move token substitution in generic TokenReplace class.

Changes: https://git.openjdk.org/jdk/pull/23923/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=23923&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8341641
  Stats: 1067 lines in 11 files changed: 1025 ins; 8 del; 34 mod
  Patch: https://git.openjdk.org/jdk/pull/23923.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/23923/head:pull/23923

PR: https://git.openjdk.org/jdk/pull/23923


More information about the core-libs-dev mailing list