-3
public static void main(String args[]) {
    try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("mvn -version");
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isrerr = new InputStreamReader(stderr);
            BufferedReader brerr = new BufferedReader(isrerr);
            InputStream stdin = proc.getInputStream();
            InputStreamReader isrin = new InputStreamReader(stdin);
            BufferedReader brin = new BufferedReader(isrin);
            String line = null;
            while ((line = brerr.readLine()) != null)
               System.out.println(line);
            while ((line = brin.readLine()) != null)
               System.out.println(line);
            int exitVal = proc.waitFor();
       } catch (Throwable t) {
            t.printStackTrace();
       }
}

this code doesn't work well,there isn't output,and the process can't stop.

help me!thank advance.I want to execute maven command in java program by invoking command line,but it output error:it will output error java.io.IOException: Cannot run program "mvn": but if i run the same command int the command line , it works well.

pkutxq
  • 43
  • 6

1 Answers1

0

You should get the process output with proc.getInputStream()

Unless you know your program is always returning trivial amounts of output, it's best to read both the error stream and the input stream in separate threads.

Then use proc.waitFor() and get your output from the reader threads.

Otherwise, your process may block, because it cannot deliver it's output.

Sample code: import java.io.*;

public class Test {

    static class StreamReader extends Thread {
        InputStream stream;
        StreamReader(InputStream stream) {
            this.stream = stream;
        }

        public void run() {
            try {
                InputStreamReader isr = new InputStreamReader(stream);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ((line = br.readLine()) != null)
                    System.out.println(line);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }

    public static void main(String args[]) {
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("mvn -version");
            new StreamReader(proc.getInputStream()).start();
            new StreamReader(proc.getErrorStream()).start();
            int exitVal = proc.waitFor();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
kaarefc
  • 206
  • 1
  • 6
  • it will output error java.io.IOException: Cannot run program "mvn": CreateProcess error=2, at java.lang.ProcessBuilder.start(ProcessBuilder.java:470) at java.lang.Runtime.exec(Runtime.java:593) at java.lang.Runtime.exec(Runtime.java:431) at java.lang.Runtime.exec(Runtime.java:328) at com.uestc.maven.MavenCMD.main(MavenCMD.java:11) but my run "mvn -version" in the command line it's OK. – pkutxq May 10 '14 at 11:07
  • Can you provide the exact code that generates that message? – kaarefc May 10 '14 at 12:01
  • I updated my answer with sample code that runs on my system – kaarefc May 10 '14 at 12:13
  • I tried your example but it still output the same error:Cannot run program "mvn": CreateProcess error=2 – pkutxq May 10 '14 at 12:52
  • Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 21:51: 28+0800) Maven home: D:\JAVASoftWare\apache-maven-3.0.5 thanks ,and this is the correct output when i run "mvn -version" in the command line. – pkutxq May 10 '14 at 12:55
  • Sounds like you don't have the Maven directory in your default PATH. You must either give the full path of mvn (probably D:\JAVASoftWare\apache-maven-3.0.5\bin\mvn) or add the directory to your PATH environment. – kaarefc May 10 '14 at 13:19
  • I set the PATH environment D:\JAVASoftWare\apache-maven-3.0.5\bin;so i can get correct output when i solely run "mvn -version" in the outside command line. – pkutxq May 11 '14 at 04:18
  • Are you running java from some IDE which was started before you set the PATH variable? – kaarefc May 11 '14 at 07:43
  • first thanks ,and I use eclipse,but i restart it many times.it output the same error. – pkutxq May 11 '14 at 07:59
  • I use a .bat file to replace the command ,and it can work now.thanks. – pkutxq May 11 '14 at 10:31