adickinson
adickinson

Reputation: 543

Wildcard in JVM options

I have a jar which is pulled in to a build via Maven, and consequently the version is included in the name of the jar, for example:

newrelic-agent-5.0.0.jar

When exporting the JVM options, in order to run the jar this file needs to be included, however this has caused a problem if we want the JVM options to be agnostic of the version number, and accept whatever version is in the build.

If the JVM options are as follows:

export JVM_OPTS="... -javaagent:/path/to/lib/newrelic-agent. ? .jar"

What can the ? be replaced with such that the JVM options find the jar in question without specifying the version of the dependency? Assuming that there will only ever be one jar that will match this file name pattern.

Upvotes: 1

Views: 644

Answers (1)

Socowi
Socowi

Reputation: 27245

No glob (*, ?, or something like that) works inside quotes ("…"). You have to unquote the part with the glob. However, "… -javaagent:/path/to/lib/newrelic-agent-"*".jar" won't work since the glob would then try to find a file starting with … -javaagent:.

Therefore, glob the jar in a separate variable and use that variable. Since there could be no or multiple versions of the library you might want to use a check and warn the user

#! /bin/bash
lib=("/path/to/lib/newrelic-agent-"*.jar)
if [ "${#lib[@]}" != 1 ]; then
    echo "Found no or multiple versions of lib newrelic agent"
    exit 1
fi
export JVM_OPTS="... -javaagent:$lib"

This works only in bash-like shells that support arrays array=(…). If you need a posix version use

#! /bin/sh
exactlyOne() {
  if [ "$#" != 1 ]; then
      echo "Found no or multiple matches"
      exit 1
  fi
  echo "$1"
}
lib=$(exactlyOne "/path/to/lib/newrelic-agent-"*.jar)
export JVM_OPTS="... -javaagent:$lib"

Upvotes: 1

Related Questions