0

I am developing a plugin for intelliJ.

I need to call some mvn commands inside my java code. But strangely, it returns me an IOexception:

Cannot run program "mvn" (in directory "/Users/ryouyasachi/Getty/semantiful-differentials-getty/getty/dsproj"):
error=2, No such file or directory

This is my code:

 /** @return: null if the process does not exit with 0
 *  @return: output if the process exits with 0
 */

public static String runCommand(String directory, List<String> command) {

    ProcessBuilder processBuilder = new ProcessBuilder(command).directory(new File(directory));

    processBuilder.redirectErrorStream(true);

    Process process;
    String output = null;
    try {
        process = processBuilder.start();


        //Pause the current thread until the process is done
        process.waitFor();

        //When the process does not exit properly
        if (process.exitValue() != 0) {

            //Error
            System.out.println("command exited in error: " + process.exitValue());

            //Handle the error
            return null;
        }else {

            output = readOutput(process);
            System.out.println(output);
        }

    } catch (InterruptedException e) {
        System.out.println("Something wrong with command: " +e.getMessage());

    } catch (IOException e) {
        System.out.println("Something wrong with command: " +e.getMessage());
    }

    return output;
}

/**
 *
 * @param process which exits with 0
 * @return The output string of the process
 */
private static String readOutput(Process process){
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));


    String line;
    StringBuilder stringBuilder = new StringBuilder();
    try {

        while ((line = in.readLine()) != null) {
            stringBuilder.append(line);
        }

        in.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return stringBuilder.toString();
}

PS: 1. I am able to run the mvn commands I need in the prompt under the project directory.This should indicate that the project directory does exist and I have maven installed properly.

2.My code runs fine with the git commands.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `No such file or directory` is pretty explicit. It cannot find the `mvn` executable. Is the path set correctly? – Jim Garrison Jul 18 '17 at 04:47
  • I think so. I am able to run the mvn in the sandbox intelliJ IDEA. `JAVA_HOME JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home export JAVA_HOME M2_HOME=/Users/ryouyasachi/apache-maven-3.5.0 export M2_HOME PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin export PATH` in my .bash_profile – Jiaxiang Liang Jul 18 '17 at 04:54

2 Answers2

1

Have you considered using the Maven Invoker instead of resorting to system calls?

The Invoker will automatically try to detect a Maven installation by evaluating the system property maven.home and the environment variable M2_HOME

They didn't say anything about PATH so I'm not sure if your particular issue is handled, but most of your code is boilerplate which is provided by the invoker out of the box.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
0

Even though mvn is in your PATH, this answer illustrates the new ProcessBuilder first argument has to be the full path of the executable called.

So try passing the full path of mvn as your first argument (the other arguments being the parameters of mvn, if you have any).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250