Search code examples
tomcatjargradlejacocojvm-arguments

Can't jar -tvf a jar file


I have a jar file and ls -ltr on the file shows me valid file size and permissions. When I open this file using 7Zip, it opens successfully and the .jar file contains a valid MANIFEST.MF file as well.

/home/u100012/koba/Infrastructure/build/tmp/expandedArchives/org.jacoco.agent-0.7.2.201409121644.jar_778m6tp3jrtvcetasufl59dmau/jacocoagent.jar

Running jar -tvf on this file, gives me the following error message:

PS: This happens if I run "jar -tvf /full/path/to/jacocoagent.jar" file but if I change directory to the full path where the jacocoagent.jar file sits, jar -tvf jacocoagent.jar does NOT give me the following error message.

java.io.FileNotFoundException: \home\u100012\koba\Infrastructure\build\tmp\expandedArchives\org.jacoco.agent-0.7.2.201409121644.jar_778m6tp3jrtvcetasufl59dmau\jacocoagent.jar (The system cannot find the path specified)
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:215)
        at java.util.zip.ZipFile.<init>(ZipFile.java:145)
        at java.util.zip.ZipFile.<init>(ZipFile.java:116)
        at sun.tools.jar.Main.list(Main.java:1004)
        at sun.tools.jar.Main.run(Main.java:245)
        at sun.tools.jar.Main.main(Main.java:1177)

7Zip is showing the following contents in the .jar file's META-INF/MANIFEST.MF file.

Manifest-Version: 1.0
Premain-Class: org.jacoco.agent.rt.internal_e6e56f0.PreMain
Archiver-Version: Plexus Archiver
Build-Jdk: 1.5.0_22
Built-By: godin
Created-By: Apache Maven
Implementation-Title: JaCoCo Java Agent
Implementation-Version: 0.7.2.201409121644
Implementation-Vendor: Mountainminds GmbH & Co. KG

When I'm using the above jacocoagent.jar file for setting javaagent in Tomcat JVM for getting Jacoco Code coverage for Integration Tests in Gradle, tomcat start script doesn't start and gives the same error message about missing manifest/file.

Catalina.out log message says:

Error occurred during initialization of VM
agent library failed to init: instrument
Error opening zip file or JAR manifest missing : /home/u100012/koba/Infrastructure/build/tmp/expandedArchives/org.jacoco.agent-0.7.2.201409121644.jar_778m6tp3jrtvcetasufl59dmau/jacocoagent.jar

My questions:

  1. Why jar -tvf fails on the jar file when 7zip and ls Unix command works / shows it's a valid file. Is this because, MANIFEST.MF file's 2nd line for Premain-Class has a different value that what I have in my workspace under build/tmp/expandedArchives folder? Gradle creates that folder on the fly. PS: This error doesn't come if I run jar -tvf jacocoagent.jar (when I'm in the directory where this file exists).

  2. What agent jar file do I need to use (jacocoagent.jar or jacoco-agent.jar), saw diff blogs saying diff file names. The one I see in the project's build folder tree is jacocoagent.jar

  3. How to resolve this error message?


Solution

  • Answer to all ?s

    1. The path for -javaagent:xxxxx/jacocoagent.jar (where xxxxx path is RELATIVE to the current workspace of the project $projectDir in Gradle). Thus, if I give, relative path of jacocoagent.jar from my current workspace location, then it works fine. Tomcat started up successfully. Wondering why it didn't work, when I used the full path (as it took // which didn't exist).

    How I found this. I

    echo `pwd`
    

    in tomcatStart.sh script just before Tomcat runs and it echoed my current dir was my workspace (where I checked out the project's code). jacocoagent.jar file to -javaagent is relative so -javaagent:somefolder/jacocoagent.jar will work, if somefolder exists in my project workspace and contains jacocoagent.jar.