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?

1

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
it produces if: BufferedReader in = new BufferedReader( new InputStreamReader(p.getInputStream()) ); String line = ""; while ((line = in.readLine()) != null) { System.out.println(line);
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).

Comments

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"

Comments

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.

2 Comments

I added C:\User\username\apache-maven-3.0.4\bin to my system PATH, but I still get the same error.
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.