7

I am trying to run maven from my java class based on this suggestion:

How to run maven from java?

Runtime.getRuntime().exec("mvn");

but I get:

java.io.IOException: Cannot run program "mvn": CreateProcess error=2, The system cannot find the file specified

mvn is on my path and I can run it just fine from cmd:

C:\Users\m>mvn -v
Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100)
Maven home: C:\apache-maven-3.0.3\bin\..
Java version: 1.6.0_20, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_20\jre
...

any suggestions?

Community
  • 1
  • 1
u123
  • 15,603
  • 58
  • 186
  • 303

4 Answers4

10

Try:

Runtime.getRuntime().exec("cmd \c mvn");

Edit: In response to the firs question...

Yes. See: Process#getInputStream. Basically you will need to consume the output from the sub-process being created.

I also like this article: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • 2
    I need to do: Runtime.getRuntime().exec("cmd /C mvn"); but I don't get any output in my console, is it necessary to pipe that out somehow? – u123 Mar 12 '12 at 20:36
  • it produces if: BufferedReader in = new BufferedReader( new InputStreamReader(p.getInputStream()) ); String line = ""; while ((line = in.readLine()) != null) { System.out.println(line); – ses Aug 15 '13 at 20:27
7

Use mvn.cmd instead of mvn or mvn.bat.
It works fine (on Windows OS).
But keep in mind the OS compatibility issue (Windows/Linux).

Codex
  • 1,153
  • 1
  • 20
  • 31
2

You can view mvn.bat and echo which java command is actually executed and run it directly.

In mvn.bat:

%MAVEN_JAVA_EXE% %MAVEN_OPTS% -classpath %CLASSWORLDS_JAR% "-Dclassworlds.conf=%M2_HOME%\bin\m2.conf" "-Dmaven.home=%M2_HOME%" %CLASSWORLDS_LAUNCHER% %MAVEN_CMD_LINE_ARGS%

In my machine executing dependency:tree is:

"java -classpath \"C:\dev\tools\apache-maven-3.1.1\boot\plexus-classworlds-2.5.1.jar\" -Dclassworlds.conf=C:\dev\tools\apache-maven-3.1.1\bin\m2.conf -Dmaven.home=\"C:\dev\tools\apache-maven-3.1.1\" org.codehaus.plexus.classworlds.launcher.Launcher dependency:tree"

ichaki5748
  • 1,993
  • 1
  • 14
  • 13
1

Error 2 means that the executable cannot be found by the JRE environment. This means that the PATH environment variable does not contain the Maven binary directory.

2 choices here:

  1. Make sure that the Maven bin directory is in the PATH environment variable
  2. Use an absolute path to the mvn command.

Alternatively, this could also be due to a permission denied, but it is less likely the case.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • 2
    I added C:\User\username\apache-maven-3.0.4\bin to my system PATH, but I still get the same error. – IgorGanapolsky Sep 27 '12 at 16:13
  • Try restarting ide and possibly your computer. I followed the instructions from Guillame as well, and also experienced the same issue after i added maven to the PATH. However, once I restarted my computer, Maven was found on the PATH and Eclipse was able to run the maven command. Hope this helps... – liltitus27 Oct 29 '13 at 20:27