在触发Gradle测试时,使用commons-exec在Java中运行命令行失败

时间:2017-09-11 15:30:25

标签: java intellij-idea gradle

它如此有线,我无法运行" npm -v"当我在Intellij中使用Gradle插件运行测试用例时

public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        DefaultExecutor executor = new DefaultExecutor();
        executor.execute(CommandLine.parse("npm -v"));
    }
}

public class MainTest {
    @Test
    public void name() throws Exception {
        Main.main(new String[]{});
    }
}

我在以下情况下一切正常:
1.用罐子启动应用程序 2.使用IntelliJ触发测试用例(右键单击并运行' MainTest')
3.运行gradle clean check

但是当我使用Gradle插件运行测试时,一切都变得不安。 enter image description here

输出

:compileJava
:processResources NO-SOURCE
:classes
:compileTestJava UP-TO-DATE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:test
me.imlc.helloworld.MainTest > name FAILED
    java.io.IOException at MainTest.java:12
        Caused by: java.io.IOException at MainTest.java:12

Cannot run program "npm" (in directory "."): error=2, No such file or directory
java.io.IOException: Cannot run program "npm" (in directory "."): error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at java.lang.Runtime.exec(Runtime.java:620)
    at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:61)
    at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:279)
    at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:336)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153)
    at me.imlc.helloworld.Main.main(Main.java:11)
    at me.imlc.helloworld.MainTest.name(MainTest.java:12)
    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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    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 org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)

如果您需要更多信息,请告诉我。 T.T

1 个答案:

答案 0 :(得分:2)

我遇到了mac机器的同样问题。

在IntellijIDEA中运行sbt时,它给出了与问题中提到的相同的错误,但是从终端运行它工作正常。

事实证明,intellijIDEA使用的不同于环境变量中的SET。

有关详细信息,请参阅博客fix PATH environment variable for IntelliJ IDEA on Mac OS X

作为解决方案,我创建了一个shell脚本

#!/usr/bin/env bash
open -a "IntelliJ IDEA"

并使其在登录refer this answer to launch an executable at login

执行

这看起来像是一个intellijIDEA问题,因为其他人也面临同样的问题,可以参考here