Gradle JRE与JDK的区别,请从您的JDK中添加lib/tools.jar库文件

3

我正在使用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的java home?我还尝试在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? - Peter Niederwieser
更新问题: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 - ioanbsu
3个回答

10

这不是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")
}

1
谢谢,Peter。在GRADLE-1477的评论中,您推荐了以下内容: compile files("${System.getProperty('java.home')}/lib/tools.jar") }``` 这似乎更简单、更安全、更易移植。您认为呢? - jcsahnwaldt Reinstate Monica

3

好的,看起来我需要使用JRE才是正确的选择。没有理由将java.home设置为JDK。 因此,我解决的方法就是将tools库添加到我的类路径中:

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

1

已编辑

看看这篇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,这是我MAC上正确的JDK路径。 - ioanbsu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接