<p>I need to add the &quot;<strong>jmx_prometheus_javaagent</strong>&quot; jar to JIB build but I get the following error when I deploy the application:</p>
<pre><code>Error opening zip file or JAR manifest missing : /srv/jmx_prometheus_javaagent-0.16.1.jar
Error occurred during initialization of VM
agent library failed to init: instrument
</code></pre>
<p>I have added the jar and a config file in the required directory and these are the relevant change I did in my <strong>build.gradle.kts</strong>:</p>
<pre><code>val jibExtraDir = file(&quot;$buildDir/jib-extra&quot;)
val jibAppRoot = &quot;/srv&quot;

val copyJmxPrometheusJavaAgent by tasks.registering(Copy::class) {
    val jarName = &quot;jmx_prometheus_javaagent-0.16.1.jar&quot;
    val configName = &quot;jmx_prometheus_config.yaml&quot;
    from(&quot;${project.projectDir.absolutePath}/resources/jmx-prometheus&quot;)
    into(&quot;$jibExtraDir/$jibAppRoot&quot;)
    doLast {
        // Required for reproducible builds.
        file(&quot;$jibExtraDir/$jibAppRoot/$jarName&quot;).setLastModified(0)
        file(&quot;$jibExtraDir/$jibAppRoot/$configName&quot;).setLastModified(0)
    }
}

subProjects {
    plugins.withType&lt;JibPlugin&gt;().configureEach {
        container {
            jvmFlags = listOf(
                           &quot;-javaagent:$jibAppRoot/jmx_prometheus_javaagent-0.16.1.jar=8099:$jibAppRoot/jmx_prometheus_config.yaml&quot;
                       )
        }
        tasks.withType&lt;JibTask&gt; {
            dependsOn(&quot;:copyJmxPrometheusJavaAgent&quot;)
        }
}
</code></pre>
<p>This is my <strong>jmx_prometheus_config.yaml</strong>:</p>
<pre><code>---
lowercaseOutputName: true
whitelistObjectNames: [
    &quot;java.lang:type=Memory&quot;,
    &quot;java.lang:type=GarbageCollector,*&quot;
]
</code></pre>
<p>The error message states that the JAR manifest is missing. But since the directory structure I added in the JIB looks alright, I am not sure what I am doing wrong here.</p>

rishav
<p>You are copying the agent files from <code>resources/jmx-prometheus/*</code> into <code>&lt;project root&gt;/build/jib-extra/srv</code>, but that just ends there. Jib isn't putting those files into a built image.</p>
<p>Since the default Jib &quot;extra directories&quot; is <code>src/main/jib</code>, technically, you can simply copy the files into <code>src/main/jib/...</code>. However, I assume you don't want to check in the files into the source directory, so you will probably want to configure the Jib extra directory like this:</p>
<pre class="lang-kotlin prettyprint-override"><code>        container {
            jvmFlags = ...
        }
        extraDirectories {
            paths {
                path {
                    setFrom(&quot;$jibExtraDir&quot;)
                }
            }
        }
</code></pre>
<p>or</p>
<pre class="lang-kotlin prettyprint-override"><code>extraDirectories.paths = &quot;$jibExtraDir&quot;
</code></pre>
<p>See this <a href="https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin#adding-arbitrary-files-to-the-image" rel="nofollow noreferrer">doc</a> for more information about the <code>extraDirectories</code> configuration.</p>
<p>Also, here's a <a href="https://github.com/GoogleContainerTools/jib/blob/master/examples/java-agent/build.gradle" rel="nofollow noreferrer">Jib sample</a> that dynamically downloads and copies a Java agent.</p>
<hr />
<p>Tip: <a href="https://github.com/wagoodman/dive" rel="nofollow noreferrer"><code>dive</code></a> is a very handy tool to inspect the contents of a build image. Use it to verify whether the files are correctly copied into an image.</p>
