Search code examples
javajava-web-startjnlpjacocojavaagents

How to measure code coverage with the JaCoCo agent for a Java Web Start (JNLP) application?


Is there a way to get the JaCoCo agent attached to a javaws Web Start (JNLP) application?

I normally run the application by calling javaws app.jnlp. Here's a sample jnlp file:

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://localhost:8000/" href="test.jnlp">
    <information>
        <title>Testing</title>
        <vendor>vendor</vendor>
        <homepage href="http://localhost:8080/" />
        <description>Testing Testing</description>
        <security>
            <all-permissions/>
        </security>
    </information>
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.6+" />
        <jar href="test.jar" />
    </resources>
    <application-desc main-class="Main" />
</jnlp>

The application itself just prints "Hello world" to the console.

I tried:

  • set the JAVAWS_VM_ARGS="-javaagent:/path/to/jacocoagent.jar" environment varuable
  • set <j2se version="1.6+" java-vm-args="-javaagent:/path/to/jacocoagent.jar" /> in the jnlp

Neither of these produces a jacoco.exec file with code coverage.

Running javaws -J-javaagent:/path/to/jacocoagent.jar app.jnlp or setting JAVA_TOOL_OPTIONS="-javaagent:/path/to/jacocoagent.jar" does seem to try to load the agent but results in:

java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.dir" "read")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
    at java.security.AccessController.checkPermission(AccessController.java:884)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1294)
    at java.lang.System.getProperty(System.java:717)
    at java.io.UnixFileSystem.resolve(UnixFileSystem.java:133)
    at java.io.File.getAbsolutePath(File.java:556)
    at java.io.File.getAbsoluteFile(File.java:572)
    at org.jacoco.agent.rt.internal_8db3ebe.output.FileOutput.startup(FileOutput.java:42)
    at org.jacoco.agent.rt.internal_8db3ebe.Agent.startup(Agent.java:122)
    at org.jacoco.agent.rt.internal_8db3ebe.Agent.getInstance(Agent.java:50)
    at org.jacoco.agent.rt.internal_8db3ebe.PreMain.premain(PreMain.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Caused by: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "shutdownHooks")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
    at java.security.AccessController.checkPermission(AccessController.java:884)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.Runtime.addShutdownHook(Runtime.java:209)
    at org.jacoco.agent.rt.internal_8db3ebe.Agent.getInstance(Agent.java:51)
    at org.jacoco.agent.rt.internal_8db3ebe.PreMain.premain(PreMain.java:45)
    ... 6 more

Is there any way to attach the JaCoCo agent to such a JNLP JVM?


Solution

  • I figured it out: adding

    grant {
        permission java.security.AllPermission;
    };
    

    to /etc/icedtea-web/javaws.policy (path differs on Windows: C:\Program Files\Java\<java-version>\lib\security\javaws.policy) got rid of the security exceptions when running either

    • javaws -J-javaagent:/path/to/jacocoagent.jar app.jnlp (only seems to work on Linux)
    • or JAVA_TOOL_OPTIONS="-javaagent:/path/to/jacocoagent.jar" javaws app.jnlp

    and the jacoco.exec file is created.

    Please also note that apparently, your application needs to be signed for this to work: https://stackoverflow.com/a/16960559/1396068


    As per the Java Web Start documentation, java-vm-args does not support java agents as only a limited number of VM args is supported which are deemed safe.