I am trying to run maven from my java class based on this suggestion:
How to run maven from java?
Runtime.getRuntime().exec(\"mvn\");
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:
Alternatively, this could also be due to a permission denied, but it is less likely the case.
User "mvn.cmd" instead of mvn or mvn.bat. It works fine.
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
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"