2

It's so wired that I can not run "npm -v" when I'm running test cases by Gradle plugin in Intellij

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[]{});
    }
}

Everything works fine when I:
1. Launch application with jar
2. Trigger test cases using IntelliJ(Right click and run 'MainTest')
3. Run gradle clean check

But when I run tests using Gradle plugin, everything become uneasy. enter image description here

The output is

: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)

Let me know if you need much information. T.T

Lawrence Ching
  • 423
  • 7
  • 16
  • Try printing `PATH` environment variable from your code and compare the results when it works and when it doesn't. Is `PATH` the same? Does it contain the path to `npm` binary? – CrazyCoder Sep 11 '17 at 16:31

1 Answers1

2

I faced the same problem for mac machine.

While running sbt from within IntellijIDEA it gave the same error as mentioned in the question but running it from the terminal worked fine.

It turns out that intellijIDEA uses different PATH than that is SET in environment variable.

For more details refer the blog fix PATH environment variable for IntelliJ IDEA on Mac OS X

As a solution I created a shell script

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

and made it execute at login refer this answer to launch an executable at login

Well this seems like an intellijIDEA problem as others have also faced the same problem which can be referred here

Saloni Vithalani
  • 323
  • 1
  • 4
  • 13