2017-09-11 45 views
0

它是如此的有線,我不能跑「NPM -v」當我在的IntelliJ無法使用公地EXEC運行命令行中使用Java時觸發搖籃測試

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

但是當我運行使用搖籃插件測試,永遠變得不安。 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

+0

嘗試打印'PATH'環境從你的代碼中剔除變量,並且在它的工作和不工作時比較結果。 PATH是否一樣?它是否包含'npm'二進制文件的路徑? – CrazyCoder

回答

1

我面對mac機器的同樣的問題。

從IntellijIDEA運行sbt時,它給出了與問題中提到的相同的錯誤,但從終端運行它工作正常。

事實證明,intellijidea使用不同於在環境變量中設置的PATH。

欲瞭解更多詳情,請參閱該博客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