如何使用 JaCoCo 代理测量 Java Web Start (JNLP) 应用程序的代码覆盖率?

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

有没有办法让 JaCoCo 代理附加到 javaws Web Start (JNLP) 应用程序?

我通常通过调用 javaws app.jnlp 来 运行 应用程序。这是一个示例 jnlp 文件:

<?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>

应用程序本身只是将 "Hello world" 打印到控制台。

我试过了:

这些都不会生成具有代码覆盖率的 jacoco.exec 文件。

运行 javaws -J-javaagent:/path/to/jacocoagent.jar app.jnlp 或设置 JAVA_TOOL_OPTIONS="-javaagent:/path/to/jacocoagent.jar" 似乎确实尝试加载代理但导致:

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

有没有办法将 JaCoCo 代理附加到这样的 JNLP JVM?

我想通了:添加

grant {
    permission java.security.AllPermission;
};

/etc/icedtea-web/javaws.policy(路径在Windows上不同:C:\Program Files\Java\<java-version>\lib\security\javaws.policy)在运行或者

时摆脱了安全异常
  • javaws -J-javaagent:/path/to/jacocoagent.jar app.jnlp(似乎只适用于 Linux)
  • JAVA_TOOL_OPTIONS="-javaagent:/path/to/jacocoagent.jar" javaws app.jnlp

并创建 jacoco.exec 文件。

另请注意,显然,您的应用程序需要签名才能运行:


根据 the Java Web Start documentationjava-vm-args 不支持 java 代理,因为仅支持有限数量的被认为安全的 VM 参数。