我用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' s? 我还尝试使用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
答案 0 :(得分:10)
它不是JDK与JRE问题。 JAVA_HOME
与java.home
的值是正确且可预期的(请参阅有关此主题的其他SO问题)。 tools.jar
是执行工具所必需的javac
和javadoc
。但是,JDK的javac
命令行编译器和Gradle的JavaCompile
任务都不会自动将tools.jar
放在用户代码的编译类路径上。相反,您必须明确添加它。例如:
def jdkHome = System.getenv("JAVA_HOME")
dependencies {
compile files("$jdkHome/lib/tools.jar")
}
答案 1 :(得分:3)
好吧,似乎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
答案 2 :(得分:1)
被修改
查看http://www.gradle.org/docs/current/userguide/build_environment.html。 特别是本节
配置按以下顺序应用(如果在多个位置配置选项,则最后一个获胜):
来自位于项目构建目录中的gradle.properties。
来自位于gradle用户主页的gradle.properties。
来自系统属性,例如在命令行中使用-Dsome.property时。