【问题标题】:Gradle JRE vs JDK please add lib/tools.jar from your JDKGradle JRE vs JDK 请从您的 JDK 添加 lib/tools.jar
【发布时间】:2014-05-31 05:48:04
【问题描述】:

我正在使用 gradle 运行 java 测试。 这是我的例外:

java.lang.RuntimeException: java.lang.IllegalStateException: Unable to load Java agent; please add lib/tools.jar from your JDK to the classpath
at org.powermock.modules.agent.PowerMockClassRedefiner.redefine(PowerMockClassRedefiner.java:59)
at org.powermock.modules.agent.support.PowerMockAgentTestInitializer.redefine(PowerMockAgentTestInitializer.java:49)
at org.powermock.modules.agent.support.PowerMockAgentTestInitializer.initialize(PowerMockAgentTestInitializer.java:41)
at com.blablacompany.app.weight.WeightMilestonesViewBeanUnitTest.setUpMock(WeightMilestonesViewBeanUnitTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:80)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:47)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Unable to load Java agent; please add lib/tools.jar from your JDK to the classpath
at org.powermock.modules.agent.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:97)
at org.powermock.modules.agent.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:70)
at org.powermock.modules.agent.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:40)
at org.powermock.modules.agent.PowerMockAgent.verifyInitialization(PowerMockAgent.java:83)
at org.powermock.modules.agent.PowerMockAgent.instrumentation(PowerMockAgent.java:76)
at org.powermock.modules.agent.PowerMockClassRedefiner.redefine(PowerMockClassRedefiner.java:57)
... 37 more

事实证明,由于某种原因,我的 gradle 使用 JRE 的文件夹作为 java.home,这就是它找不到 tools.jar 的原因。 我不认为我想手动将它添加到我的类路径中。我想知道是否有任何方法可以告诉 gradle 使用 JDK 的 java home 而不是 JRE? 我还尝试在 gradle.properties 文件中使用 org.gradle.java.home 覆盖它,但它不起作用。非常感谢任何帮助。

当我从我的 IDE (Intellij IDEA) 运行相同的测试时,所有测试都成功通过。我在 Mac OS 上运行所有这些。

println System.getenv("JAVA_HOME")
println System.properties['java.home']

显示:

/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home                                                                                                                                                                
/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/jre 

【问题讨论】:

  • 将以下内容添加到构建脚本时会打印什么? println System.getenv("JAVA_HOME") 哪个操作系统/JDK?
  • 更新了问题:println System.getenv("JAVA_HOME") -->> 打印 /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home。 println System.properties['java.home'] -->> 打印 /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/jre

标签: java unit-testing gradle powermock


【解决方案1】:

这不是 JDK 与 JRE 的问题。 JAVA_HOMEjava.home 的值是正确且符合预期的(请参阅有关此主题的其他 SO 问题)。 tools.jar执行 javacjavadoc 等工具所必需的。但是,JDK 的javac 命令行编译器和Gradle 的JavaCompile 任务都不会自动将tools.jar 放在用户代码的编译类路径上。相反,您必须明确添加它。例如:

def jdkHome = System.getenv("JAVA_HOME")

dependencies {
    compile files("$jdkHome/lib/tools.jar")
}

【讨论】:

  • 谢谢,彼得。在a comment on GRADLE-1477 中,您推荐了以下内容:dependencies { compile files("${System.getProperty('java.home')}/lib/tools.jar") },这看起来更简单、更安全、更便携。你怎么看?
【解决方案2】:

好吧,看来 JRE 实际上是我需要使用的正确的一个。没有理由将 java.home 设置为 JDK。 所以我解决它的方法只是将工具库添加到我的类路径中:

testCompile ([fileTree(dir: "${System.properties['java.home']}/../lib", include: '*tools.jar'),fileTree(dir: "lib/test", include: '*.jar') ])

最初我不想这样做的原因是,一旦我添加了它,我就有一堆

java 7 errors: java.lang.VerifyError: Expecting a stackmap frame at branch target 10

我确实通过在我的测试闭包中添加“-noverify”参数来修复它:

Closure basicTestConfiguration = {
jvmArgs "-Dactivemq.directory=${testActivemqDir}",
        "-Duser.timezone=Etc/UTC",
        "-javaagent:${configurations.testAgent.singleFile}",
        "-XX:MaxPermSize=256m",
        "-noverify"
...
}

并将这个闭包应用到我的测试套件:

postCommitSuite basicTestConfiguration

【讨论】:

    【解决方案3】:

    编辑

    查看http://www.gradle.org/docs/current/userguide/build_environment.html。 尤其是这一段

    配置按以下顺序应用(如果一个选项在多个位置配置,最后一个获胜):


    来自位于项目构建目录中的 gradle.properties。
    来自位于 gradle 用户主页中的 gradle.properties。
    来自系统属性,例如在命令行中使用 -Dsome.property 时。

    【讨论】:

    • 我想我做到了,因为 System.getenv("JAVA_HOME") 将其打印到控制台:/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home,这是正确的 JDK 路径在我的 MAC 上。
    猜你喜欢
    • 2011-05-21
    • 2018-04-18
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多