Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Maven from Java code in Windows?

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?

like image 996
u123 Avatar asked Mar 12 '12 20:03

u123


1 Answers

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

like image 121
Tim Bender Avatar answered Sep 28 '22 06:09

Tim Bender